From 8ca77fb9e7de35422f9422f37877e7dfadd3a0f5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 12 Nov 2025 07:37:15 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2654 --- .../README.md | 39 +++++++++++++++ .../README_EN.md | 49 ++++++++++++++++++- .../Solution.rs | 34 +++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/Solution.rs diff --git a/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README.md b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README.md index 3c9486ab851ac..3fca2e41b6bfb 100644 --- a/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README.md +++ b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README.md @@ -231,6 +231,45 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len() as i32; + let cnt = nums.iter().filter(|&&x| x == 1).count() as i32; + if cnt > 0 { + return n - cnt; + } + let mut mi = n + 1; + for i in 0..nums.len() { + let mut g = 0; + for j in i..nums.len() { + g = gcd(g, nums[j]); + if g == 1 { + mi = mi.min((j - i + 1) as i32); + break; + } + } + } + if mi > n { + -1 + } else { + n - 1 + mi - 1 + } + } +} + +fn gcd(mut a: i32, mut b: i32) -> i32 { + while b != 0 { + let tmp = a % b; + a = b; + b = tmp; + } + a.abs() +} +``` + diff --git a/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README_EN.md b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README_EN.md index 89e788cf442fd..8e93d90ce0051 100644 --- a/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README_EN.md +++ b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README_EN.md @@ -65,7 +65,15 @@ tags: -### Solution 1 +### Solution 1: Math + +We first count the number of $1$s in the array $nums$ as $cnt$. If $cnt \gt 0$, then we only need $n - cnt$ operations to turn the entire array into $1$s. + +Otherwise, we need to first turn one element in the array into $1$, and then the minimum number of remaining operations is $n - 1$. + +Consider how to turn one element in the array into $1$ while minimizing the number of operations. In fact, we only need to find a minimum contiguous subarray interval $nums[i,..j]$ such that the greatest common divisor of all elements in the subarray is $1$, with the subarray length being $mi = \min(mi, j - i + 1)$. Finally, our total number of operations is $n - 1 + mi - 1$. + +The time complexity is $O(n \times (n + \log M))$ and the space complexity is $O(\log M)$, where $n$ and $M$ are the length of the array $nums$ and the maximum value in the array $nums$, respectively. @@ -223,6 +231,45 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len() as i32; + let cnt = nums.iter().filter(|&&x| x == 1).count() as i32; + if cnt > 0 { + return n - cnt; + } + let mut mi = n + 1; + for i in 0..nums.len() { + let mut g = 0; + for j in i..nums.len() { + g = gcd(g, nums[j]); + if g == 1 { + mi = mi.min((j - i + 1) as i32); + break; + } + } + } + if mi > n { + -1 + } else { + n - 1 + mi - 1 + } + } +} + +fn gcd(mut a: i32, mut b: i32) -> i32 { + while b != 0 { + let tmp = a % b; + a = b; + b = tmp; + } + a.abs() +} +``` + diff --git a/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/Solution.rs b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/Solution.rs new file mode 100644 index 0000000000000..b76fee540a5e0 --- /dev/null +++ b/solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/Solution.rs @@ -0,0 +1,34 @@ +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let n = nums.len() as i32; + let cnt = nums.iter().filter(|&&x| x == 1).count() as i32; + if cnt > 0 { + return n - cnt; + } + let mut mi = n + 1; + for i in 0..nums.len() { + let mut g = 0; + for j in i..nums.len() { + g = gcd(g, nums[j]); + if g == 1 { + mi = mi.min((j - i + 1) as i32); + break; + } + } + } + if mi > n { + -1 + } else { + n - 1 + mi - 1 + } + } +} + +fn gcd(mut a: i32, mut b: i32) -> i32 { + while b != 0 { + let tmp = a % b; + a = b; + b = tmp; + } + a.abs() +}