-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
26 lines (21 loc) · 764 Bytes
/
solution.java
File metadata and controls
26 lines (21 loc) · 764 Bytes
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
class Solution {
public int maximumUnits(int[][] boxTypes, int truckSize) {
//Sort in decreasing order of number of units
Arrays.sort(boxTypes, (a,b)-> Integer.valueOf(b[1]).compareTo(Integer.valueOf(a[1])));
int result = 0;
int boxIdx = 0;
while(boxIdx < boxTypes.length && truckSize > 0){
int box = boxTypes[boxIdx][0];
int unit = boxTypes[boxIdx][1];
if(truckSize - box > 0){
result += (box * unit);
truckSize -= box;
} else {
result += (truckSize * unit);
truckSize = 0;
}
boxIdx++;
}
return result;
}
}