File tree Expand file tree Collapse file tree 1 file changed +6
-9
lines changed
solution/1700-1799/1710.Maximum Units on a Truck Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -290,16 +290,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
290
290
291
291
``` ts
292
292
function maximumUnits(boxTypes : number [][], truckSize : number ): number {
293
- const cnt = new Array (1001 ).fill (0 );
294
- for (const [a, b] of boxTypes ) {
295
- cnt [b ] += a ;
296
- }
293
+ boxTypes .sort (([_ , a ], [__ , b ]) => b - a );
297
294
let ans = 0 ;
298
- for (let b = 1000 ; b > 0 && truckSize > 0 ; -- b ) {
299
- const a = cnt [ b ] ;
300
- if ( a > 0 ) {
301
- ans += b * Math . min (truckSize , a );
302
- truckSize -= a ;
295
+ for (const [count, size] of boxTypes ) {
296
+ ans += Math . min ( truckSize , count ) * size ;
297
+ truckSize -= count ;
298
+ if (truckSize < 0 ) {
299
+ break ;
303
300
}
304
301
}
305
302
return ans ;
You can’t perform that action at this time.
0 commit comments