We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0b9c52c commit 4a75195Copy full SHA for 4a75195
src/MaxUnitsOnATTruck.java
@@ -0,0 +1,23 @@
1
+import java.util.Arrays;
2
+
3
+//https://leetcode.com/problems/maximum-units-on-a-truck/
4
5
+class MaxUnitsOnATTruck {
6
+ public int maximumUnits(int[][] boxTypes, int truckSize) {
7
+ //Using Comparator to sort in decending order
8
+ //sort 2d array with max units boxes to be on top
9
+ Arrays.sort(boxTypes, (a, b) -> b[1]-a[1]);
10
+ int ts=0,nob=0;
11
12
+ //Until truck is filled add the boxes
13
+ for(int i=0;i<boxTypes.length;i++) {
14
+ if(nob+boxTypes[i][0]>=truckSize) {
15
+ return ts+(truckSize-nob)*boxTypes[i][1];
16
+ } else {
17
+ nob+=boxTypes[i][0];
18
+ ts+=(boxTypes[i][1]*boxTypes[i][0]);
19
+ }
20
21
+ return ts;
22
23
+}
0 commit comments