8000 Sync LeetCode submission - Maximum Units on a Truck (java) · thatbeautifuldream/leetcode-sync@1b1471d · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b1471d

Browse files
Sync LeetCode submission - Maximum Units on a Truck (java)
1 parent 346b77e commit 1b1471d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maximumUnits(int[][] boxTypes, int truckSize) {
3+
Arrays.sort(boxTypes, (a, b) -> -Integer.compare(a[1], b[1]));
4+
int maxUnits = 0;
5+
for (int[] box : boxTypes) {
6+
if (truckSize < box[0]) {
7+
return maxUnits + truckSize * box[1];
8+
}
9+
maxUnits += box[0] * box[1];
10+
truckSize -= box[0];
11+
}
12+
13+
return maxUnits;
14+
}
15+
}

0 commit comments

Comments
 (0)
0