File tree 3 files changed +33
-2
lines changed
solution/2100-2199/2171.Removing Minimum Number of Magic Beans
3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 78
78
### ** TypeScript**
79
79
80
80
``` 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
+ };
82
92
```
83
93
84
94
### ** ...**
Original file line number Diff line number Diff line change @@ -70,7 +70,17 @@ There are no other solutions that removes 7 beans or fewer.
70
70
### ** TypeScript**
71
71
72
72
``` 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
+ };
74
84
```
75
85
76
86
### ** ...**
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments