Show problems as
Classic problem #1710EasyArrayGreedySorting

Maximum Units on a Truck

Real-world version: Seedling Trolley Load
Tracked in this browser only
Read Maximum Units on a Truck on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A nursery wheels plant trays out to the loading dock on a single trolley.

You are given trayTypes, where trayTypes[i] = [count_i, seedlings_i] means the nursery has count_i trays of type i on hand and each of those trays holds exactly seedlings_i seedlings.

The trolley has room for trolleySlots trays altogether. You may put on any mix of types, taking no more of a type than the nursery has, and you are free to leave slots empty.

Return the greatest number of seedlings a single trolley run can carry.

Examples

Example 1

Input
trayTypes = [[2,4],[3,7],[1,2]], trolleySlots = 4
Output
25

Loading all three trays of type 1 at 7 seedlings each and one tray of type 0 at 4 seedlings uses 4 slots and carries 21 + 4 = 25 seedlings.

Example 2

Input
trayTypes = [[6,3],[2,9]], trolleySlots = 2
Output
18

Only 2 slots exist, and loading both trays of type 1 carries 9 + 9 = 18 seedlings.

Example 3

Input
trayTypes = [[1,5],[2,8]], trolleySlots = 10
Output
21

Every tray the nursery has fits, so all 3 trays go on and carry 5 + 8 + 8 = 21 seedlings, leaving 7 slots empty.

Example 4

Input
trayTypes = [[4,6],[5,6],[2,1]], trolleySlots = 7
Output
42

Types 0 and 1 both hold 6 seedlings per tray, and taking all 4 trays of type 0 plus 3 trays of type 1 fills the 7 slots for 42 seedlings.

Constraints

  • 1 <= trayTypes.length <= 1000
  • trayTypes[i].length == 2
  • 1 <= count_i, seedlings_i <= 1000
  • 1 <= trolleySlots <= 10^6

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def maximum_units(trayTypes: list[list[int]], trolleySlots: int) -> int:
Java
public int maximumUnits(int[][] trayTypes, int trolleySlots)