@@ -63,13 +63,41 @@ We can remove it with 1 deletion.
63
63
### ** Python3**
64
64
65
65
``` 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)
67
77
```
68
78
69
79
### ** Java**
70
80
71
81
``` 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
+ }
73
101
```
74
102
75
103
### ** TypeScript**
@@ -82,13 +110,60 @@ function minimumDeletions(nums: number[]): number {
82
110
let j = nums .indexOf (Math .max (... nums ));
83
111
let left = Math .min (i , j );
84
112
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
113
return Math .min (left + 1 + n - right , right + 1 , n - left );
89
114
};
90
115
```
91
116
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
+
92
167
### ** ...**
93
168
94
169
```
0 commit comments