Similar Problems
Similar Problems not available
Maximum Units On A Truck - Leetcode Solution
LeetCode: Maximum Units On A Truck Leetcode Solution
Difficulty: Easy
Problem Statement:
You are given two arrays, boxTypes and truckSize, of positive integers where boxTypes[i] = [numberOfBoxes[i], numberOfUnitsPerBox[i]] represents the number of boxes of type i and the number of units in each box of type i, respectively. You are also given an integer truckSize, which represents 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.
Solution:
The solution to this problem involves sorting the given boxTypes array in non-increasing order based on the number of units per box. After sorting the array, we can traverse through the array, and for each box type, keep adding boxes to the truck until the truck is full. In each iteration, we keep track of the number of boxes and units added to the truck and update it until the truck is full. The algorithm for the solution is as follows:
-
Sort the boxTypes array in non-increasing order based on the number of units per box.
-
Initialize two variables, total_units and total_boxes, as 0.
-
Traverse through the boxTypes array and for each box type, do the following:
a. Check if the number of remaining boxes can be added to the truck without exceeding the truck size.
b. If so, add the remaining boxes and units to the truck and update the total_boxes and total_units variables.
c. If not, add only the remaining space on the truck and update the total_boxes and total_units variables.
-
Return the value of the total_units variable.
The time complexity of this algorithm is O(nlogn) due to the sorting operation, where n is the length of the boxTypes array. The space complexity is O(1) as we are not using any extra space.
Here is the Python implementation of the solution:
def maximumUnits(boxTypes, truckSize): # Sort the boxTypes array in non-increasing order based on the number of units per box boxTypes.sort(key = lambda x: x[1], reverse = True)
# Initialize variables
total_units = 0
total_boxes = 0
# Traverse through the boxTypes array and add boxes to the truck
for boxes, units in boxTypes:
# Check if the remaining boxes can be added to the truck without exceeding the truck size
if total_boxes + boxes <= truckSize:
total_boxes += boxes
total_units += boxes * units
# If not, add only the remaining space on the truck
else:
remaining_boxes = truckSize - total_boxes
total_boxes += remaining_boxes
total_units += remaining_boxes * units
break
# Return the maximum total number of units that can be put on the truck
return total_units
Example usage
boxTypes = [[1, 3], [2, 2], [3, 1]] truckSize = 4 print(maximumUnits(boxTypes, truckSize)) # Output: 8
Explanation: We can take 1 box of type 1 (3 units per box) and 2 boxes of type 2 (2 units per box) for a total of (1 * 3) + (2 * 2) = 8 units. There is no space for any boxes of type 3.
Maximum Units On A Truck Solution Code
1