Skip to content

Commit 3449c17

Browse files
authored
Added Maximum Units on a Truck.java
1 parent 078b216 commit 3449c17

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Easy/Maximum Units on a Truck.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
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;
7+
}
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;
17+
}
18+
return maximumUnits;
19+
}
20+
}

0 commit comments

Comments
 (0)