-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumUnitsOnATruck.java
More file actions
54 lines (53 loc) · 1.99 KB
/
MaximumUnitsOnATruck.java
File metadata and controls
54 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* LeetCode problem 1710. Maximum Units on a Truck: https://leetcode.com/problems/maximum-units-on-a-truck/
*/
public class Solution {
/**
* Finds the total units that the given truck can carry
* Time Complexity: O(N + M), where N = length of boxTypes, and M = truckSize
* <p>
* Space Complexity: O(N), where N = length of boxTypes
*
* @param boxTypes the boxes to be loaded (boxAmount, unitsPerBox)
* @param truckSize the number of boxes that can be loaded
* @return the total units that can be carried by the truck
*/
public int maximumUnits(int[][] boxTypes, int truckSize) {
// get the largest of numberOfUnitsPerBox, add as many as possible, find the next largest etc. maximize
// do this until all the boxes have been added or there isn't any space left on truck
int spaceLeft = truckSize;
int total = 0;
Set<Integer> added = new HashSet<>();
while (spaceLeft > 0 && added.size() != boxTypes.length) {
// find the largest numberOfUnitsPerBox
int largest = findLargestUnits(boxTypes, added);
int noOfBoxes = boxTypes[largest][0];
int units = boxTypes[largest][1];
// increment total until possible
while (noOfBoxes > 0 && spaceLeft > 0) {
spaceLeft -= 1;
noOfBoxes -= 1;
total += units;
}
// mark this index as seen
added.add(largest);
}
return total;
}
/**
* Finds the index of the largest number of units per box
*
* @param boxTypes
* @return
*/
public int findLargestUnits(int[][] boxTypes, Set<Integer> added) {
int largest = 0;
for (int i = 0; i < boxTypes.length; i++) {
if (added.contains(largest)) largest = i;
if (boxTypes[i][1] >= boxTypes[largest][1] && !added.contains(i)) {
largest = i;
}
}
return largest;
}
}