Skip to content

Commit 4bb8c0b

Browse files
authored
Update README.md
1 parent 4cbb78c commit 4bb8c0b

File tree

1 file changed

+6
-9
lines changed
  • solution/1700-1799/1710.Maximum Units on a Truck

1 file changed

+6
-9
lines changed

solution/1700-1799/1710.Maximum Units on a Truck/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
291291

292292
```ts
293293
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);
298295
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;
304301
}
305302
}
306303
return ans;

0 commit comments

Comments
 (0)