Skip to content

Commit 4a75195

Browse files
author
Aravind Vella
committed
Daily Challenge July 1
1 parent 0b9c52c commit 4a75195

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/MaxUnitsOnATTruck.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)