Skip to content

Commit 973ee6a

Browse files
committed
feat: add solutions to lc problem: No.2091
No.2091.Removing Minimum and Maximum From Array
1 parent 503b6be commit 973ee6a

File tree

6 files changed

+240
-7
lines changed

6 files changed

+240
-7
lines changed

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

+90-2
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,60 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
先找出最小值和最大值的下标 mi, mx。如果 mi 下标大于 mx,则将 mx 与 mi 两数进行交换。
68+
69+
最小删除的次数,共有 3 种情况:
70+
71+
1. 从左侧往右依次删除 `nums[mi]``nums[mx]`
72+
1. 从右侧往左依次删除 `nums[mx]``nums[mi]`
73+
1. 从左侧往右删除 `nums[mi]`,从右侧往左删除 `nums[mx]`
74+
75+
求这 3 种情况的最小值即可。
76+
6777
<!-- tabs:start -->
6878

6979
### **Python3**
7080

7181
<!-- 这里可写当前语言的特殊实现逻辑 -->
7282

7383
```python
74-
84+
class Solution:
85+
def minimumDeletions(self, nums: List[int]) -> int:
86+
mi = mx = 0
87+
for i, num in enumerate(nums):
88+
if num < nums[mi]:
89+
mi = i
90+
if num > nums[mx]:
91+
mx = i
92+
if mi > mx:
93+
mi, mx = mx, mi
94+
return min(mx + 1, len(nums) - mi, mi + 1 + len(nums) - mx)
7595
```
7696

7797
### **Java**
7898

7999
<!-- 这里可写当前语言的特殊实现逻辑 -->
80100

81101
```java
82-
102+
class Solution {
103+
public int minimumDeletions(int[] nums) {
104+
int mi = 0, mx = 0, n = nums.length;
105+
for (int i = 0; i < n; ++i) {
106+
if (nums[i] < nums[mi]) {
107+
mi = i;
108+
}
109+
if (nums[i] > nums[mx]) {
110+
mx = i;
111+
}
112+
}
113+
if (mi > mx) {
114+
int t = mx;
115+
mx = mi;
116+
mi = t;
117+
}
118+
return Math.min(Math.min(mx + 1, n - mi), mi + 1 + n - mx);
119+
}
120+
}
83121
```
84122

85123
### **TypeScript**
@@ -99,6 +137,56 @@ function minimumDeletions(nums: number[]): number {
99137
};
100138
```
101139

140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int minimumDeletions(vector<int>& nums) {
146+
int mi = 0, mx = 0, n = nums.size();
147+
for (int i = 0; i < n; ++i)
148+
{
149+
if (nums[i] < nums[mi]) mi = i;
150+
if (nums[i] > nums[mx]) mx = i;
151+
}
152+
if (mi > mx)
153+
{
154+
int t = mi;
155+
mi = mx;
156+
mx = t;
157+
}
158+
return min(min(mx + 1, n - mi), mi + 1 + n - mx);
159+
}
160+
};
161+
```
162+
163+
### **Go**
164+
165+
```go
166+
func minimumDeletions(nums []int) int {
167+
mi, mx, n := 0, 0, len(nums)
168+
for i, num := range nums {
169+
if num < nums[mi] {
170+
mi = i
171+
}
172+
if num > nums[mx] {
173+
mx = i
174+
}
175+
}
176+
if mi > mx {
177+
mi, mx = mx, mi
178+
}
179+
return min(min(mx+1, n-mi), mi+1+n-mx)
180+
}
181+
182+
func min(a, b int) int {
183+
if a < b {
184+
return a
185+
}
186+
return b
187+
}
188+
```
189+
102190
### **...**
103191

104192
```

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

+80-5
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,41 @@ We can remove it with 1 deletion.
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def minimumDeletions(self, nums: List[int]) -> int:
68+
mi = mx = 0
69+
for i, num in enumerate(nums):
70+
if num < nums[mi]:
71+
mi = i
72+
if num > nums[mx]:
73+
mx = i
74+
if mi > mx:
75+
mi, mx = mx, mi
76+
return min(mx + 1, len(nums) - mi, mi + 1 + len(nums) - mx)
6777
```
6878

6979
### **Java**
7080

7181
```java
72-
82+
class Solution {
83+
public int minimumDeletions(int[] nums) {
84+
int mi = 0, mx = 0, n = nums.length;
85+
for (int i = 0; i < n; ++i) {
86+
if (nums[i] < nums[mi]) {
87+
mi = i;
88+
}
89+
if (nums[i] > nums[mx]) {
90+
mx = i;
91+
}
92+
}
93+
if (mi > mx) {
94+
int t = mx;
95+
mx = mi;
96+
mi = t;
97+
}
98+
return Math.min(Math.min(mx + 1, n - mi), mi + 1 + n - mx);
99+
}
100+
}
73101
```
74102

75103
### **TypeScript**
@@ -82,13 +110,60 @@ function minimumDeletions(nums: number[]): number {
82110
let j = nums.indexOf(Math.max(...nums));
83111
let left = Math.min(i, j);
84112
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
88113
return Math.min(left + 1 + n - right, right + 1, n - left);
89114
};
90115
```
91116

117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int minimumDeletions(vector<int>& nums) {
123+
int mi = 0, mx = 0, n = nums.size();
124+
for (int i = 0; i < n; ++i)
125+
{
126+
if (nums[i] < nums[mi]) mi = i;
127+
if (nums[i] > nums[mx]) mx = i;
128+
}
129+
if (mi > mx)
130+
{
131+
int t = mi;
132+
mi = mx;
133+
mx = t;
134+
}
135+
return min(min(mx + 1, n - mi), mi + 1 + n - mx);
136+
}
137+
};
138+
```
139+
140+
### **Go**
141+
142+
```go
143+
func minimumDeletions(nums []int) int {
144+
mi, mx, n := 0, 0, len(nums)
145+
for i, num := range nums {
146+
if num < nums[mi] {
147+
mi = i
148+
}
149+
if num > nums[mx] {
150+
mx = i
151+
}
152+
}
153+
if mi > mx {
154+
mi, mx = mx, mi
155+
}
156+
return min(min(mx+1, n-mi), mi+1+n-mx)
157+
}
158+
159+
func min(a, b int) int {
160+
if a < b {
161+
return a
162+
}
163+
return b
164+
}
165+
```
166+
92167
### **...**
93168

94169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int minimumDeletions(vector<int>& nums) {
4+
int mi = 0, mx = 0, n = nums.size();
5+
for (int i = 0; i < n; ++i)
6+
{
7+
if (nums[i] < nums[mi]) mi = i;
8+
if (nums[i] > nums[mx]) mx = i;
9+
}
10+
if (mi > mx)
11+
{
12+
int t = mi;
13+
mi = mx;
14+
mx = t;
15+
}
16+
return min(min(mx + 1, n - mi), mi + 1 + n - mx);
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minimumDeletions(nums []int) int {
2+
mi, mx, n := 0, 0, len(nums)
3+
for i, num := range nums {
4+
if num < nums[mi] {
5+
mi = i
6+
}
7+
if num > nums[mx] {
8+
mx = i
9+
}
10+
}
11+
if mi > mx {
12+
mi, mx = mx, mi
13+
}
14+
return min(min(mx+1, n-mi), mi+1+n-mx)
15+
}
16+
17+
func min(a, b int) int {
18+
if a < b {
19+
return a
20+
}
21+
return b
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minimumDeletions(int[] nums) {
3+
int mi = 0, mx = 0, n = nums.length;
4+
for (int i = 0; i < n; ++i) {
5+
if (nums[i] < nums[mi]) {
6+
mi = i;
7+
}
8+
if (nums[i] > nums[mx]) {
9+
mx = i;
10+
}
11+
}
12+
if (mi > mx) {
13+
int t = mx;
14+
mx = mi;
15+
mi = t;
16+
}
17+
return Math.min(Math.min(mx + 1, n - mi), mi + 1 + n - mx);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minimumDeletions(self, nums: List[int]) -> int:
3+
mi = mx = 0
4+
for i, num in enumerate(nums):
5+
if num < nums[mi]:
6+
mi = i
7+
if num > nums[mx]:
8+
mx = i
9+
if mi > mx:
10+
mi, mx = mx, mi
11+
return min(mx + 1, len(nums) - mi, mi + 1 + len(nums) - mx)

0 commit comments

Comments
 (0)