Skip to content

Commit 920c0c4

Browse files
committedFeb 19, 2022
feat: add typescript solution to lc problem: No.2171
No.2171.Removing Minimum Number of Magic Beans
1 parent dfbcf69 commit 920c0c4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed
 

‎solution/2100-2199/2171.Removing Minimum Number of Magic Beans/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@
7878
### **TypeScript**
7979

8080
```ts
81-
81+
function minimumRemoval(beans: number[]): number {
82+
const n = beans.length;
83+
let sum = beans.reduce((a, c) => a + c, 0);
84+
beans.sort((a, b) => a - b);
85+
let ans = sum;
86+
for (let i = 0; i < n; i++) {
87+
let num = beans[i];
88+
ans = Math.min(sum - num * (n - i), ans);
89+
}
90+
return ans;
91+
};
8292
```
8393

8494
### **...**

‎solution/2100-2199/2171.Removing Minimum Number of Magic Beans/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ There are no other solutions that removes 7 beans or fewer.
7070
### **TypeScript**
7171

7272
```ts
73-
73+
function minimumRemoval(beans: number[]): number {
74+
const n = beans.length;
75+
let sum = beans.reduce((a, c) => a + c, 0);
76+
beans.sort((a, b) => a - b);
77+
let ans = sum;
78+
for (let i = 0; i < n; i++) {
79+
let num = beans[i];
80+
ans = Math.min(sum - num * (n - i), ans);
81+
}
82+
return ans;
83+
};
7484
```
7585

7686
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minimumRemoval(beans: number[]): number {
2+
const n = beans.length;
3+
let sum = beans.reduce((a, c) => a + c, 0);
4+
beans.sort((a, b) => a - b);
5+
let ans = sum;
6+
for (let i = 0; i < n; i++) {
7+
let num = beans[i];
8+
ans = Math.min(sum - num * (n - i), ans);
9+
}
10+
return ans;
11+
};

0 commit comments

Comments
 (0)
Please sign in to comment.