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