-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_1710.java
70 lines (60 loc) · 2.28 KB
/
prob_1710.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Arrays;
import java.util.Comparator;
/**
* 1710. Maximum Units on a Truck
* <p>
* Easy
* <p>
* You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]:
* <p>
* numberOfBoxes[i] is the number of boxes of type i.
* numberOfUnitsPerBox[i] is the number of units in each box of the type i.
* You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck.
* You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
* <p>
* Return the maximum total number of units that can be put on the truck.
*/
public class prob_1710 {
public static void main(String[] args) {
Solution_1710 solution = new Solution_1710();
// int[][] boxTypes = {{1, 3}, {2, 2}, {3, 1}};
// int truckSize = 4;
int[][] boxTypes = {{5, 10}, {2, 5}, {4, 7}, {3, 9}};
int truckSize = 10;
System.out.println(solution.maximumUnits(boxTypes, truckSize));
}
}
class Solution_1710 {
/**
* Greedy solution - using sort
* <p>
* Here the point to observe is that to get the maximum # of units, we need to first add the boxes with the larger # of units first into the truck to maximize the capacity
* <p>
* Time Complexity - O(NLogN), Space Complexity - O(1)
*/
public int maximumUnits(int[][] boxTypes, int truckSize) {
Arrays.sort(boxTypes, this.generateBoxTypeComparator());
int sumUnits = 0;
for (int[] boxType : boxTypes) {
if (truckSize == 0) break;
if (truckSize > boxType[0]) {
truckSize -= boxType[0];
sumUnits += boxType[0] * boxType[1];
} else {
sumUnits += truckSize * boxType[1];
break;
}
}
return sumUnits;
}
/**
* Greedy solution - using bucket sort
*/
public int maximumUnits_v2(int[][] boxTypes, int truckSize) {
// TODO - Implement an optimization to the above method by using bucket sort
return -1;
}
Comparator<int[]> generateBoxTypeComparator() {
return Comparator.comparing(ints -> ints, (b1, b2) -> b2[1] - b1[1]);
}
}