Maximize the total units loaded onto a truck that holds a limited number of boxes

Quick Overview

Given several box types, each with a box count and a number of units per box, and a truck that holds a limited number of boxes, find the most units the truck can carry. It tests recognizing when a greedy choice is optimal, sorting by the right key, and capping each pick at the remaining capacity.

Maximize the total units loaded onto a truck that holds a limited number of boxes

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are assigned to put some amount of boxes onto one truck. You are given a 2D array `box_types`, where `box_types[i] = [number_of_boxes_i, units_per_box_i]`: - `number_of_boxes_i` is the number of boxes of type `i`. - `units_per_box_i` is the number of units in each box of type `i`. You are also given an integer `truck_size`, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed `truck_size`. Return the maximum total number of units that can be put on the truck. ### Function Signature ```python def maximum_units(box_types: list[list[int]], truck_size: int) -> int: ``` ### Constraints - `1 <= len(box_types) <= 1000` - `1 <= number_of_boxes_i, units_per_box_i <= 1000` - `1 <= truck_size <= 10^6` ### Examples ```text Input: box_types = [[1,3],[2,2],[3,1]], truck_size = 4 Output: 8 ``` Take the 1 box of the first type (3 units), both boxes of the second type (2 * 2 = 4 units) and 1 box of the third type (1 unit). Total = 3 + 4 + 1 = 8 units. ```text Input: box_types = [[5,10],[2,5],[4,7],[3,9]], truck_size = 10 Output: 91 ``` Take 5 boxes with 10 units each (50), 3 boxes with 9 units each (27) and 2 boxes with 7 units each (14). Total = 50 + 27 + 14 = 91 units. ```text Input: box_types = [[2,4],[1,6]], truck_size = 10 Output: 14 ``` Only 3 boxes exist, fewer than the truck can hold, so all of them go on: 1 * 6 + 2 * 4 = 14 units.

Overview: Given several box types, each with a box count and a number of units per box, and a truck that holds a limited number of boxes, find the most units the truck can carry. It tests recognizing when a greedy choice is optimal, sorting by the right key, and capping each pick at the remaining capacity.

Amazon logo
Amazon
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are assigned to put some amount of boxes onto one truck. You are given a 2D array box_types, where box_types[i] = [number_of_boxes_i, units_per_box_i]:

  • number_of_boxes_i is the number of boxes of type i .
  • units_per_box_i is the number of units in each box of type i .

You are also given an integer truck_size, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truck_size.

Return the maximum total number of units that can be put on the truck.

Function Signature

def maximum_units(box_types: list[list[int]], truck_size: int) -> int:

Constraints

  • 1 <= len(box_types) <= 1000
  • 1 <= number_of_boxes_i, units_per_box_i <= 1000
  • 1 <= truck_size <= 10^6

Examples

Input: box_types = [[1,3],[2,2],[3,1]], truck_size = 4
Output: 8

Take the 1 box of the first type (3 units), both boxes of the second type (2 * 2 = 4 units) and 1 box of the third type (1 unit). Total = 3 + 4 + 1 = 8 units.

Input: box_types = [[5,10],[2,5],[4,7],[3,9]], truck_size = 10
Output: 91

Take 5 boxes with 10 units each (50), 3 boxes with 9 units each (27) and 2 boxes with 7 units each (14). Total = 50 + 27 + 14 = 91 units.

Input: box_types = [[2,4],[1,6]], truck_size = 10
Output: 14

Only 3 boxes exist, fewer than the truck can hold, so all of them go on: 1 * 6 + 2 * 4 = 14 units.