1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public int maximumUnits(int[][] boxTypes, int truckSize) { Arrays.sort(boxTypes, (a, b) -> (b[1] - a[1])); int ans = 0, remaining = truckSize; for (int i = 0; i < boxTypes.length && remaining > 0; i++) { if (boxTypes[i][0] <= remaining) { ans += boxTypes[i][0] * boxTypes[i][1]; remaining -= boxTypes[i][0]; } else { ans += remaining * boxTypes[i][1]; remaining = 0; } } return ans; } }
|