Maximum Units on a Truck

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prem Parmar

Prem Parmar

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