mediumMixed

Minimize Cargo Transports Solution

Problem Statement

Given a list of crate sizes and their respective quantities, determine the minimum number of transports required to move all crates using a fleet of vehicles, each with a capacity of 95 units.

Examples

Example 1:
Input:[10, 20, 30] and quantities [2, 3, 1]
Output:6
Explanation: Total cargo is (10*2 + 20*3 + 30*1) = 20 + 60 + 30 = 110 units. Each wagon can carry 85 units of cargo. The minimum number of trips is calculated based on this total and the wagon capacity.
Example 2:
Input:[5, 5, 5] and quantities [5, 5, 5]
Output:9
Explanation: Total cargo is (5*5 + 5*5 + 5*5) = 25 + 25 + 25 = 75 units. Since the quantities are the same, and the wagon capacity is 85 units, we calculate the total units per destination as 5*5 = 25 units. We would need 3 trips to transport all crates from one destination, and there are 3 destinations, resulting in 3*3 = 9 trips.

Constraints

  • 1 <= number of crate sizes <= 100
  • 1 <= quantity of each crate size <= 1000
  • 1 <= size of each crate <= 100
Time: O(n log n) Space: O(n)
The optimal approach involves using a greedy algorithm to fill each transport vehicle with the largest crates first, which minimizes the number of transports required. This approach has a time complexity of O(n log n) due to the sorting of crate sizes. By utilizing this strategy, the algorithm can efficiently determine the minimum number of transports.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

import sys crateSizes = [] quantities = [] for line in sys.stdin: size, quantity = map(int, line.split()) crateSizes.append(size) quantities.append(quantity) totalTransports = 0 for i in range(len(crateSizes)): totalTransports += -(-quantities[i] * crateSizes[i] // 95) print(totalTransports)