Skip to content

Commit 0e27969

Browse files
authored
Update Maximum Units on a Truck.java
1 parent 0f13529 commit 0e27969

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

Easy/Maximum Units on a Truck.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
class Solution {
22
public int maximumUnits(int[][] boxTypes, int truckSize) {
3-
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> {
4-
int c = o2[1] - o1[1];
5-
if (c != 0) {
6-
return c;
3+
Arrays.sort(boxTypes, (o1, o2) -> o2[1] - o1[1]);
4+
int maxUnits = 0;
5+
for (int[] boxType : boxTypes) {
6+
if (truckSize == 0) {
7+
break;
78
}
8-
return o2[0] - o1[0];
9-
});
10-
Collections.addAll(pq, boxTypes);
11-
int maximumUnits = 0;
12-
while (truckSize > 0 && !pq.isEmpty()) {
13-
int[] boxType = pq.poll();
14-
int boxesAdded = Math.min(boxType[0], truckSize);
15-
maximumUnits += boxesAdded * boxType[1];
16-
truckSize -= boxesAdded;
9+
int numberOfBoxesLoaded = Math.min(truckSize, boxType[0]);
10+
maxUnits += numberOfBoxesLoaded * boxType[1];
11+
truckSize -= numberOfBoxesLoaded;
1712
}
18-
return maximumUnits;
13+
return maxUnits;
1914
}
2015
}

0 commit comments

Comments
 (0)