Load a Truck with the Most Units

Quick Overview

Load a Truck with the Most Units. Each row of boxTypes gives a number of available boxes and the units contained in each box of that type. The truck holds at most truckSize boxes regardless of type.

Load a Truck with the Most Units

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Each row of `boxTypes` gives a number of available boxes and the units contained in each box of that type. The truck holds at most `truckSize` boxes regardless of type. Choose boxes to maximize their total units and return that total. ```cpp class Solution { public: int maximumUnits(vector<vector<int>>& boxTypes, int truckSize); }; ``` ### Examples **Example 1:** ```text boxTypes = [[2,5],[3,2]], truckSize = 3 Output: 12 Explanation: Take both five-unit boxes and one two-unit box. ``` **Example 2:** ```text boxTypes = [[1,8],[2,3]], truckSize = 5 Output: 14 Explanation: All three available boxes fit. ``` ### Constraints - `1 <= boxTypes.length <= 1000`. - Each box count and units-per-box value is in `[1,1000]`. - `1 <= truckSize <= 10^6`. ### Clarifying Questions The capacity counts boxes, not units or weight. Taking fewer than all boxes of a type is allowed. ```hint Inspect the structure What does one loaded box consume from capacity, and which quantities may be taken only in part? ``` ### Reasoning checkpoints - Respect both per-type availability and the truck's box-count capacity. - Prove maximal units even when the final selection uses only part of a type's stock. ### Optional extensions How could a frequency table over the bounded units-per-box range replace sorting?

Overview: Load a Truck with the Most Units. Each row of boxTypes gives a number of available boxes and the units contained in each box of that type. The truck holds at most truckSize boxes regardless of type.

Infosys logo
Infosys
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each row of boxTypes gives a number of available boxes and the units contained in each box of that type. The truck holds at most truckSize boxes regardless of type. Choose boxes to maximize their total units and return that total.

class Solution {
public:
    int maximumUnits(vector<vector<int>>& boxTypes, int truckSize);
};

Examples

Example 1:

boxTypes = [[2,5],[3,2]], truckSize = 3
Output: 12
Explanation: Take both five-unit boxes and one two-unit box.

Example 2:

boxTypes = [[1,8],[2,3]], truckSize = 5
Output: 14
Explanation: All three available boxes fit.

Constraints

  • 1 <= boxTypes.length <= 1000 .
  • Each box count and units-per-box value is in [1,1000] .
  • 1 <= truckSize <= 10^6 .

Clarifying Questions Guidance

The capacity counts boxes, not units or weight. Taking fewer than all boxes of a type is allowed.

Reasoning checkpoints

  • Respect both per-type availability and the truck's box-count capacity.
  • Prove maximal units even when the final selection uses only part of a type's stock.

Optional extensions

How could a frequency table over the bounded units-per-box range replace sorting?