Skip to content

Commit e87fa08

Browse files
authored
feat: update ts solution to lc problem: No.1710 (doocs#3192)
1 parent a0f8841 commit e87fa08

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

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

+11-19
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
151151
#### TypeScript
152152

153153
```ts
154-
function maximumUnits(boxTypes: number[][], truckSize: number): number {
155-
boxTypes.sort((a, b) => b[1] - a[1]);
156-
let sum = 0;
154+
export function maximumUnits(boxTypes: number[][], truckSize: number): number {
155+
boxTypes.sort(([_, a], [__, b]) => b - a);
157156
let ans = 0;
158157
for (const [count, size] of boxTypes) {
159-
if (sum + count < truckSize) {
160-
ans += size * count;
161-
sum += count;
162-
} else {
163-
ans += (truckSize - sum) * size;
164-
break;
165-
}
158+
ans += Math.min(truckSize, count) * size;
159+
truckSize -= count;
160+
if (truckSize < 0) break;
166161
}
167162
return ans;
168163
}
@@ -296,16 +291,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
296291

297292
```ts
298293
function maximumUnits(boxTypes: number[][], truckSize: number): number {
299-
const cnt = new Array(1001).fill(0);
300-
for (const [a, b] of boxTypes) {
301-
cnt[b] += a;
302-
}
294+
boxTypes.sort(([_, a], [__, b]) => b - a);
303295
let ans = 0;
304-
for (let b = 1000; b > 0 && truckSize > 0; --b) {
305-
const a = cnt[b];
306-
if (a > 0) {
307-
ans += b * Math.min(truckSize, a);
308-
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;
309301
}
310302
}
311303
return ans;

Diff for: solution/1700-1799/1710.Maximum Units on a Truck/README_EN.md

+11-19
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
150150
#### TypeScript
151151

152152
```ts
153-
function maximumUnits(boxTypes: number[][], truckSize: number): number {
154-
boxTypes.sort((a, b) => b[1] - a[1]);
155-
let sum = 0;
153+
export function maximumUnits(boxTypes: number[][], truckSize: number): number {
154+
boxTypes.sort(([_, a], [__, b]) => b - a);
156155
let ans = 0;
157156
for (const [count, size] of boxTypes) {
158-
if (sum + count < truckSize) {
159-
ans += size * count;
160-
sum += count;
161-
} else {
162-
ans += (truckSize - sum) * size;
163-
break;
164-
}
157+
ans += Math.min(truckSize, count) * size;
158+
truckSize -= count;
159+
if (truckSize < 0) break;
165160
}
166161
return ans;
167162
}
@@ -295,16 +290,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
295290

296291
```ts
297292
function maximumUnits(boxTypes: number[][], truckSize: number): number {
298-
const cnt = new Array(1001).fill(0);
299-
for (const [a, b] of boxTypes) {
300-
cnt[b] += a;
301-
}
293+
boxTypes.sort(([_, a], [__, b]) => b - a);
302294
let ans = 0;
303-
for (let b = 1000; b > 0 && truckSize > 0; --b) {
304-
const a = cnt[b];
305-
if (a > 0) {
306-
ans += b * Math.min(truckSize, a);
307-
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;
308300
}
309301
}
310302
return ans;

Diff for: solution/1700-1799/1710.Maximum Units on a Truck/Solution.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
function maximumUnits(boxTypes: number[][], truckSize: number): number {
2-
boxTypes.sort((a, b) => b[1] - a[1]);
3-
let sum = 0;
2+
boxTypes.sort(([_, a], [__, b]) => b - a);
43
let ans = 0;
54
for (const [count, size] of boxTypes) {
6-
if (sum + count < truckSize) {
7-
ans += size * count;
8-
sum += count;
9-
} else {
10-
ans += (truckSize - sum) * size;
5+
ans += Math.min(truckSize, count) * size;
6+
truckSize -= count;
7+
if (truckSize < 0) {
118
break;
129
}
1310
}

0 commit comments

Comments
 (0)