bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1710. Maximum Units on a Truck

1710. Maximum Units on a Truck

Leetcode Solutions

Sorting and Greedy Approach

  1. Sort the boxTypes array in descending order based on the number of units per box.
  2. Initialize a variable totalUnits to 0 to keep track of the total number of units added to the truck.
  3. Iterate through the sorted boxTypes array. a. Calculate the number of boxes that can be added to the truck: min(remainingTruckSize, numberOfBoxesi). b. Update totalUnits by adding the product of the number of boxes added and numberOfUnitsPerBoxi. c. Decrease truckSize by the number of boxes added. d. If truckSize reaches 0, break out of the loop as the truck is full.
  4. Return totalUnits as the maximum total number of units that can be put on the truck.
UML Thumbnail

Using Priority Queue (Max Heap)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR