Skip to content

Commit 35ee416

Browse files
authored
feat: add rust solution to lc problem: No.2009 (#1789)
1 parent 94d498d commit 35ee416

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,35 @@ public:
208208
};
209209
```
210210

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+
211240
### **Go**
212241

213242
```go

solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,35 @@ public:
181181
};
182182
```
183183

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+
184213
### **Go**
185214

186215
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)