Maximum Units on a Truck

Prem Parmar
Competitive Programming Problems
2 min readJul 1, 2022

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

  • numberOfBoxesi is the number of boxes of type i.
  • numberOfUnitsPerBoxi is the number of units in each box of the type i.

You are also given an integer truckSize, 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 truckSize.

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

Let us deep dive in the question, We have 1 truck which has capacity that it won’t carry more than x Boxes in it (~ Ignore about units only think about boxes).

First we will check how many units are there is the box and after that will sort it in the decreasing order using Comparator.

Looping over the array and as per the truck space we will add boxes in truck.

Here below I have attached Program, Please take a look and comment me if you have any doubt or suggestion.

--

--

Prem Parmar
Competitive Programming Problems

Software Engineer, having 3 years of experience in Ecommerce / HCM domain Product based company.