File tree 3 files changed +46
-0
lines changed
solution/2000-2099/2091.Removing Minimum and Maximum From Array
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 82
82
83
83
```
84
84
85
+ ### ** TypeScript**
86
+
87
+ ``` ts
88
+ function minimumDeletions(nums : number []): number {
89
+ const n = nums .length ;
90
+ if (n == 1 ) return 1 ;
91
+ let i = nums .indexOf (Math .min (... nums ));
92
+ let j = nums .indexOf (Math .max (... nums ));
93
+ let left = Math .min (i , j );
94
+ let right = Math .max (i , j );
95
+ // 左右 left + 1 + n - right
96
+ // 两个都是左边 left + 1 + right - left = right + 1
97
+ // 都是右边 n - right + right - left = n - left
98
+ return Math .min (left + 1 + n - right , right + 1 , n - left );
99
+ };
100
+ ```
101
+
85
102
### ** ...**
86
103
87
104
```
Original file line number Diff line number Diff line change @@ -72,6 +72,23 @@ We can remove it with 1 deletion.
72
72
73
73
```
74
74
75
+ ### ** TypeScript**
76
+
77
+ ``` ts
78
+ function minimumDeletions(nums : number []): number {
79
+ const n = nums .length ;
80
+ if (n == 1 ) return 1 ;
81
+ let i = nums .indexOf (Math .min (... nums ));
82
+ let j = nums .indexOf (Math .max (... nums ));
83
+ let left = Math .min (i , j );
84
+ let right = Math .max (i , j );
85
+ // 左右 left + 1 + n - right
86
+ // 两个都是左边 left + 1 + right - left = right + 1
87
+ // 都是右边 n - right + right - left = n - left
88
+ return Math .min (left + 1 + n - right , right + 1 , n - left );
89
+ };
90
+ ```
91
+
75
92
### ** ...**
76
93
77
94
```
Original file line number Diff line number Diff line change
1
+ function minimumDeletions ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
3
+ if ( n == 1 ) return 1 ;
4
+ let i = nums . indexOf ( Math . min ( ...nums ) ) ;
5
+ let j = nums . indexOf ( Math . max ( ...nums ) ) ;
6
+ let left = Math . min ( i , j ) ;
7
+ let right = Math . max ( i , j ) ;
8
+ // 左右 left + 1 + n - right
9
+ // 两个都是左边 left + 1 + right - left = right + 1
10
+ // 都是右边 n - right + right - left = n - left
11
+ return Math . min ( left + 1 + n - right , right + 1 , n - left ) ;
12
+ } ;
You can’t perform that action at this time.
0 commit comments