Skip to content

Commit fb0a953

Browse files
authored
feat: add typescript solution to lc problem: No.2091 (#619)
No.2091. Removing Minimum and Maximum From Array
1 parent 24e3158 commit fb0a953

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

solution/2000-2099/2091.Removing Minimum and Maximum From Array/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@
8282

8383
```
8484

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+
85102
### **...**
86103

87104
```

solution/2000-2099/2091.Removing Minimum and Maximum From Array/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ We can remove it with 1 deletion.
7272

7373
```
7474

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+
7592
### **...**
7693

7794
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};

0 commit comments

Comments
 (0)