-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumUnitsOnATruck.java
More file actions
53 lines (44 loc) · 1.43 KB
/
MaximumUnitsOnATruck.java
File metadata and controls
53 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Greedy_Algorithm_Problems;
import java.sql.Array;
import java.util.Arrays;
import java.util.Comparator;
public class MaximumUnitsOnATruck {
public static void main(String[] args) {
int[][] first = {
{1,3},
{2,2},
{3,1}
};
int truckSize1 = 4;
int[][] second = {
{5,10},
{2,5},
{4,7},
{3,9}
};
int truckSize2 = 10;
System.out.println(maximumUnits(first, truckSize1)); //8
System.out.println(maximumUnits(second, truckSize2)); // 91
}
public static int maximumUnits(int[][] boxTypes, int truckSize) {
int ans = 0;
double[][] ratio = new double[boxTypes.length][2];
for (int i=0; i< boxTypes.length; i++){
ratio[i][0] = i;
ratio[i][1] = boxTypes[i][1]/(double)boxTypes[i][0];
}
Arrays.sort(ratio, Comparator.comparingDouble(o -> o[1]));
for (int i=ratio.length-1; i>=0; i--){
int index = (int)ratio[i][0];
if (truckSize >= boxTypes[index][0]){
ans += boxTypes[index][1];
truckSize -= boxTypes[index][0];
}else{
ans += truckSize * ratio[i][1];
truckSize = 0;
break;
}
}
return ans;
}
}