-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumUnitsonaTruck.java
More file actions
35 lines (27 loc) · 980 Bytes
/
Copy pathMaximumUnitsonaTruck.java
File metadata and controls
35 lines (27 loc) · 980 Bytes
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
package arrays;
import utils.LeetCode;
import utils.Level;
import java.util.Arrays;
@LeetCode(no = 1710,
level = Level.EASY,
title = "Maximum Units on a Truck",
url = "https://leetcode.com/problems/maximum-units-on-a-truck/")
public class MaximumUnitsonaTruck {
public int maximumUnits(int[][] boxTypes, int truckSize) {
int max = 0;
Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]);
for (int i = 0; i < boxTypes.length; i++) {
int boxes[] = boxTypes[i];
int numberOfBoxes = boxes[0];
int numberOfUnitsPerBox = boxes[1];
if (numberOfBoxes <= truckSize) {
max += numberOfBoxes * numberOfUnitsPerBox;
truckSize -= numberOfBoxes;
} else if (numberOfBoxes > truckSize && truckSize > 0) {
max += truckSize * numberOfUnitsPerBox;
truckSize -= truckSize;
}
}
return max;
}
}