Skip to content

Commit

Permalink
feat: update ts solution to lc problem: No.1710 (#3192)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored Jul 3, 2024
1 parent a0f8841 commit e87fa08
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
30 changes: 11 additions & 19 deletions solution/1700-1799/1710.Maximum Units on a Truck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
#### TypeScript

```ts
function maximumUnits(boxTypes: number[][], truckSize: number): number {
boxTypes.sort((a, b) => b[1] - a[1]);
let sum = 0;
export function maximumUnits(boxTypes: number[][], truckSize: number): number {
boxTypes.sort(([_, a], [__, b]) => b - a);
let ans = 0;
for (const [count, size] of boxTypes) {
if (sum + count < truckSize) {
ans += size * count;
sum += count;
} else {
ans += (truckSize - sum) * size;
break;
}
ans += Math.min(truckSize, count) * size;
truckSize -= count;
if (truckSize < 0) break;
}
return ans;
}
Expand Down Expand Up @@ -296,16 +291,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {

```ts
function maximumUnits(boxTypes: number[][], truckSize: number): number {
const cnt = new Array(1001).fill(0);
for (const [a, b] of boxTypes) {
cnt[b] += a;
}
boxTypes.sort(([_, a], [__, b]) => b - a);
let ans = 0;
for (let b = 1000; b > 0 && truckSize > 0; --b) {
const a = cnt[b];
if (a > 0) {
ans += b * Math.min(truckSize, a);
truckSize -= a;
for (const [count, size] of boxTypes) {
ans += Math.min(truckSize, count) * size;
truckSize -= count;
if (truckSize < 0) {
break;
}
}
return ans;
Expand Down
30 changes: 11 additions & 19 deletions solution/1700-1799/1710.Maximum Units on a Truck/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {
#### TypeScript

```ts
function maximumUnits(boxTypes: number[][], truckSize: number): number {
boxTypes.sort((a, b) => b[1] - a[1]);
let sum = 0;
export function maximumUnits(boxTypes: number[][], truckSize: number): number {
boxTypes.sort(([_, a], [__, b]) => b - a);
let ans = 0;
for (const [count, size] of boxTypes) {
if (sum + count < truckSize) {
ans += size * count;
sum += count;
} else {
ans += (truckSize - sum) * size;
break;
}
ans += Math.min(truckSize, count) * size;
truckSize -= count;
if (truckSize < 0) break;
}
return ans;
}
Expand Down Expand Up @@ -295,16 +290,13 @@ func maximumUnits(boxTypes [][]int, truckSize int) (ans int) {

```ts
function maximumUnits(boxTypes: number[][], truckSize: number): number {
const cnt = new Array(1001).fill(0);
for (const [a, b] of boxTypes) {
cnt[b] += a;
}
boxTypes.sort(([_, a], [__, b]) => b - a);
let ans = 0;
for (let b = 1000; b > 0 && truckSize > 0; --b) {
const a = cnt[b];
if (a > 0) {
ans += b * Math.min(truckSize, a);
truckSize -= a;
for (const [count, size] of boxTypes) {
ans += Math.min(truckSize, count) * size;
truckSize -= count;
if (truckSize < 0) {
break;
}
}
return ans;
Expand Down
11 changes: 4 additions & 7 deletions solution/1700-1799/1710.Maximum Units on a Truck/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
function maximumUnits(boxTypes: number[][], truckSize: number): number {
boxTypes.sort((a, b) => b[1] - a[1]);
let sum = 0;
boxTypes.sort(([_, a], [__, b]) => b - a);
let ans = 0;
for (const [count, size] of boxTypes) {
if (sum + count < truckSize) {
ans += size * count;
sum += count;
} else {
ans += (truckSize - sum) * size;
ans += Math.min(truckSize, count) * size;
truckSize -= count;
if (truckSize < 0) {
break;
}
}
Expand Down

0 comments on commit e87fa08

Please sign in to comment.