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 @@ -291,16 +291,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
291
291
292
292
``` ts
293
293
function maximumUnits(boxTypes : number [][], truckSize : number ): number {
294
- const cnt = new Array (1001 ).fill (0 );
295
- for (const [a, b] of boxTypes ) {
296
- cnt [b ] += a ;
297
- }
294
+ boxTypes .sort (([_ , a ], [__ , b ]) => b - a );
298
295
let ans = 0 ;
299
- for (let b = 1000 ; b > 0 && truckSize > 0 ; -- b ) {
300
- const a = cnt [ b ] ;
301
- if ( a > 0 ) {
302
- ans += b * Math . min (truckSize , a );
303
- truckSize -= a ;
296
+ for (const [count, size] of boxTypes ) {
297
+ ans += Math . min ( truckSize , count ) * size ;
298
+ truckSize -= count ;
299
+ if (truckSize < 0 ) {
300
+ break ;
304
301
}
305
302
}
306
303
return ans ;
You can’t perform that action at this time.
0 commit comments