File tree 1 file changed +9
-14
lines changed
1 file changed +9
-14
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maximumUnits (int [][] boxTypes , int truckSize ) {
3
- PriorityQueue <int []> pq = new PriorityQueue <>((o1 , o2 ) -> {
4
- int c = o2 [1 ] - o1 [1 ];
5
- if (c != 0 ) {
6
- return c ;
3
+ Arrays .sort (boxTypes , (o1 , o2 ) -> o2 [1 ] - o1 [1 ]);
4
+ int maxUnits = 0 ;
5
+ for (int [] boxType : boxTypes ) {
6
+ if (truckSize == 0 ) {
7
+ break ;
7
8
}
8
- return o2 [0 ] - o1 [0 ];
9
- });
10
- Collections .addAll (pq , boxTypes );
11
- int maximumUnits = 0 ;
12
- while (truckSize > 0 && !pq .isEmpty ()) {
13
- int [] boxType = pq .poll ();
14
- int boxesAdded = Math .min (boxType [0 ], truckSize );
15
- maximumUnits += boxesAdded * boxType [1 ];
16
- truckSize -= boxesAdded ;
9
+ int numberOfBoxesLoaded = Math .min (truckSize , boxType [0 ]);
10
+ maxUnits += numberOfBoxesLoaded * boxType [1 ];
11
+ truckSize -= numberOfBoxesLoaded ;
17
12
}
18
- return maximumUnits ;
13
+ return maxUnits ;
19
14
}
20
15
}
You can’t perform that action at this time.
0 commit comments