Optimized Shipment Container Loading
In a bustling Shiprocket logistics hub, efficient container loading is paramount for timely deliveries and optimal resource utilization. Parcels arrive continuously and need to be grouped into outgoing containers, each with a specific weight capacity. Crucially, some parcels are marked as "priority" and require preferential treatment. This problem challenges you to design a system that intelligently loads parcels into containers, prioritizing urgent shipments while maximizing container utilization and strictly adhering to weight limits.
Your task is to implement a function that simulates the process of loading parcels into containers based on their arrival order, weight, and priority status.
loadContainers that takes an array of Parcel objects and the maxWeightCapacity of a single container.parcels array.isPriority = true) should always be considered for loading before non-priority parcels, as long as they fit into the current container. Among fitting priority parcels, pick the one that arrived earliest.isPriority = false) that fit, again picking the earliest arriving one among fitting standard parcels.parcelIds of parcels placed into it.The function signature will be:
function loadContainers(parcels: Parcel[], maxWeightCapacity: number): number[][]
parcels: An array of Parcel objects. Each Parcel object has the following structure:
interface Parcel {
parcelId: number;
weight: number;
isPriority: boolean;
}
parcelId: A unique numeric identifier for the parcel.weight: The weight of the parcel in units.isPriority: A boolean indicating if the parcel is a priority shipment.maxWeightCapacity: A number representing the maximum total weight a single container can hold.
Returns: number[][] - A list of arrays. Each inner array represents a container and contains the parcelIds of the parcels loaded into it. The order of parcelIds within an inner array does not matter, but the order of inner arrays (containers) should reflect the sequence in which they were filled.
parcels array.maxWeightCapacity.parcels array is empty, the function should return an empty list [].maxWeightCapacity will be positive integers.Examples
Example 1
Input:
parcels = [{"parcelId":1,"weight":10,"isPriority":false},{"parcelId":2,"weight":5,"isPriority":true},{"parcelId":3,"weight":12,"isPriority":false}], maxWeightCapacity = 20
Output:
[[2,1],[3]]
Note:
priorityWaiting = [P2 (5)], standardWaiting = [P1 (10), P3 (12)].currentWeight = 5. Container: [2]. priorityWaiting = [].currentWeight = 15. Container: [2, 1]. standardWaiting = [P3 (12)].[2, 1].currentWeight = 12. Container: [3]. standardWaiting = [].[3].[[2, 1], [3]].Example 2
Input:
parcels = [{"parcelId":101,"weight":30,"isPriority":true},{"parcelId":102,"weight":25,"isPriority":true},{"parcelId":103,"weight":15,"isPriority":true}], maxWeightCapacity = 50
Output:
[[101,103],[102]]
Note:
priorityWaiting = [P101 (30), P102 (25), P103 (15)], standardWaiting = [].currentWeight = 30. Container: [101]. priorityWaiting = [P102 (25), P103 (15)].currentWeight = 45. Container: [101, 103]. priorityWaiting = [P102 (25)].standardWaiting is empty.[101, 103].currentWeight = 25. Container: [102]. priorityWaiting = [].standardWaiting is empty.[102].[[101, 103], [102]].Example 3
Input:
parcels = [], maxWeightCapacity = 100
Output:
[]
Note:
No parcels are provided in the input, so no containers are loaded.
Constraints
1 <= parcels.length <= 20001 <= parcel.weight <= maxWeightCapacity (A single parcel will not exceed container capacity).1 <= maxWeightCapacity <= 5000parcelId will be unique positive integers.parcels are available for processing at the start.Loading editor...