diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md index fbdc642b2cb48..16caf6be49e3a 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md @@ -208,6 +208,35 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::BTreeSet; + +impl Solution { + #[allow(dead_code)] + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len(); + let nums = nums.into_iter().collect::>(); + + let m = nums.len(); + let nums = nums.into_iter().collect::>(); + + let mut ans = n; + + for i in 0..m { + let j = match nums.binary_search(&(nums[i] + n as i32)) { + Ok(idx) => idx, + Err(idx) => idx, + }; + ans = std::cmp::min(ans, n - (j - i)) + } + + ans as i32 + } +} +``` + ### **Go** ```go diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md index f7e463cce41f3..842488ab3784a 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md @@ -181,6 +181,35 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::BTreeSet; + +impl Solution { + #[allow(dead_code)] + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len(); + let nums = nums.into_iter().collect::>(); + + let m = nums.len(); + let nums = nums.into_iter().collect::>(); + + let mut ans = n; + + for i in 0..m { + let j = match nums.binary_search(&(nums[i] + n as i32)) { + Ok(idx) => idx, + Err(idx) => idx, + }; + ans = std::cmp::min(ans, n - (j - i)) + } + + ans as i32 + } +} +``` + ### **Go** ```go diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs new file mode 100644 index 0000000000000..7e0043590f303 --- /dev/null +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.rs @@ -0,0 +1,24 @@ +use std::collections::BTreeSet; + +impl Solution { + #[allow(dead_code)] + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len(); + let nums = nums.into_iter().collect::>(); + + let m = nums.len(); + let nums = nums.into_iter().collect::>(); + + let mut ans = n; + + for i in 0..m { + let j = match nums.binary_search(&(nums[i] + n as i32)) { + Ok(idx) => idx, + Err(idx) => idx, + }; + ans = std::cmp::min(ans, n - (j - i)) + } + + ans as i32 + } +} \ No newline at end of file