Maximum Units on a Truck

You must load boxes onto one truck. boxTypes[i] = [numberOfBoxes_i, numberOfUnitsPerBox_i]: there are numberOfBoxes_i boxes of type i, and each holds numberOfUnitsPerBox_i units. truckSize is the most boxes the truck can carry. Choose any boxes as long as their count does not exceed truckSize, and return the maximum total number of units the truck can carry.

30:00

Maximum Units on a Truck
easy
Free written solution
Topics
Companies

You must load boxes onto one truck. boxTypes[i] = [numberOfBoxes_i, numberOfUnitsPerBox_i]: there are numberOfBoxes_i boxes of type i, and each holds numberOfUnitsPerBox_i units. truckSize is the most boxes the truck can carry. Choose any boxes as long as their count does not exceed truckSize, and return the maximum total number of units the truck can carry.

Example 1:
Input: {"boxTypes":[[1,3],[2,2],[3,1]],"truckSize":4}
Output: 8
Constraints:
  • 1≤boxTypes.length≤10001 \leq \text{boxTypes.length} \leq 1000

  • 1≤numberOfBoxesi,numberOfUnitsPerBoxi≤10001 \leq \text{numberOfBoxes}_i, \text{numberOfUnitsPerBox}_i \leq 1000

  • 1≤truckSize≤1061 \leq \text{truckSize} \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"boxTypes":[[1,3],[2,2],[3,1]],"truckSize":4}

3 box types and room for 4 boxes. Every box takes one slot no matter what it holds, so slots should go to the richest boxes.

box types, input order

[boxes, units per box]

[1,3]

#0

[2,2]

#1

[3,1]

#2

truck

0 / 4 slots used

units loaded

0

Variables
VariableValue
truckSize4
units0
DepthFunction Call
Stack empty
0/9