Maximum Units on a Truck LeetCode solution in Java

1
class Solution {
2
public int maximumUnits(int[][] boxTypes, int truckSize) {
3
Arrays.sort(boxTypes, Comparator.comparingInt(o -> -o[1]));
4
int ans = 0, i = 0, n = boxTypes.length;
5
while (i < n && truckSize > 0) {
6
int maxi = Math.min(boxTypes[i][0], truckSize);
7
ans += maxi * boxTypes[i][1];
8
i++;
9
truckSize -= maxi;
10
}
11
return ans;
12
}
13
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0