File tree 3 files changed +82
-0
lines changed
solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -208,6 +208,35 @@ public:
208
208
};
209
209
```
210
210
211
+ ### ** Rust**
212
+
213
+ ``` rust
214
+ use std :: collections :: BTreeSet ;
215
+
216
+ impl Solution {
217
+ #[allow(dead_code)]
218
+ pub fn min_operations (nums : Vec <i32 >) -> i32 {
219
+ let n = nums . len ();
220
+ let nums = nums . into_iter (). collect :: <BTreeSet <i32 >>();
221
+
222
+ let m = nums . len ();
223
+ let nums = nums . into_iter (). collect :: <Vec <i32 >>();
224
+
225
+ let mut ans = n ;
226
+
227
+ for i in 0 .. m {
228
+ let j = match nums . binary_search (& (nums [i ] + n as i32 )) {
229
+ Ok (idx ) => idx ,
230
+ Err (idx ) => idx ,
231
+ };
232
+ ans = std :: cmp :: min (ans , n - (j - i ))
233
+ }
234
+
235
+ ans as i32
236
+ }
237
+ }
238
+ ```
239
+
211
240
### ** Go**
212
241
213
242
``` go
Original file line number Diff line number Diff line change @@ -181,6 +181,35 @@ public:
181
181
};
182
182
```
183
183
184
+ ### ** Rust**
185
+
186
+ ``` rust
187
+ use std :: collections :: BTreeSet ;
188
+
189
+ impl Solution {
190
+ #[allow(dead_code)]
191
+ pub fn min_operations (nums : Vec <i32 >) -> i32 {
192
+ let n = nums . len ();
193
+ let nums = nums . into_iter (). collect :: <BTreeSet <i32 >>();
194
+
195
+ let m = nums . len ();
196
+ let nums = nums . into_iter (). collect :: <Vec <i32 >>();
197
+
198
+ let mut ans = n ;
199
+
200
+ for i in 0 .. m {
201
+ let j = match nums . binary_search (& (nums [i ] + n as i32 )) {
202
+ Ok (idx ) => idx ,
203
+ Err (idx ) => idx ,
204
+ };
205
+ ans = std :: cmp :: min (ans , n - (j - i ))
206
+ }
207
+
208
+ ans as i32
209
+ }
210
+ }
211
+ ```
212
+
184
213
### ** Go**
185
214
186
215
``` go
Original file line number Diff line number Diff line change
1
+ use std:: collections:: BTreeSet ;
2
+
3
+ impl Solution {
4
+ #[ allow( dead_code) ]
5
+ pub fn min_operations ( nums : Vec < i32 > ) -> i32 {
6
+ let n = nums. len ( ) ;
7
+ let nums = nums. into_iter ( ) . collect :: < BTreeSet < i32 > > ( ) ;
8
+
9
+ let m = nums. len ( ) ;
10
+ let nums = nums. into_iter ( ) . collect :: < Vec < i32 > > ( ) ;
11
+
12
+ let mut ans = n;
13
+
14
+ for i in 0 ..m {
15
+ let j = match nums. binary_search ( & ( nums[ i] + n as i32 ) ) {
16
+ Ok ( idx) => idx,
17
+ Err ( idx) => idx,
18
+ } ;
19
+ ans = std:: cmp:: min ( ans, n - ( j - i) )
20
+ }
21
+
22
+ ans as i32
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments