Skip to content

Commit 8ca77fb

Browse files
committed
feat: add solutions to lc problem: No.2654
1 parent aefb395 commit 8ca77fb

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,45 @@ function gcd(a: number, b: number): number {
231231
}
232232
```
233233

234+
#### Rust
235+
236+
```rust
237+
impl Solution {
238+
pub fn min_operations(nums: Vec<i32>) -> i32 {
239+
let n = nums.len() as i32;
240+
let cnt = nums.iter().filter(|&&x| x == 1).count() as i32;
241+
if cnt > 0 {
242+
return n - cnt;
243+
}
244+
let mut mi = n + 1;
245+
for i in 0..nums.len() {
246+
let mut g = 0;
247+
for j in i..nums.len() {
248+
g = gcd(g, nums[j]);
249+
if g == 1 {
250+
mi = mi.min((j - i + 1) as i32);
251+
break;
252+
}
253+
}
254+
}
255+
if mi > n {
256+
-1
257+
} else {
258+
n - 1 + mi - 1
259+
}
260+
}
261+
}
262+
263+
fn gcd(mut a: i32, mut b: i32) -> i32 {
264+
while b != 0 {
265+
let tmp = a % b;
266+
a = b;
267+
b = tmp;
268+
}
269+
a.abs()
270+
}
271+
```
272+
234273
<!-- tabs:end -->
235274

236275
<!-- solution:end -->

solution/2600-2699/2654.Minimum Number of Operations to Make All Array Elements Equal to 1/README_EN.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Math
69+
70+
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.
71+
72+
Otherwise, we need to first turn one element in the array into $1$, and then the minimum number of remaining operations is $n - 1$.
73+
74+
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$.
75+
76+
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.
6977

7078
<!-- tabs:start -->
7179

@@ -223,6 +231,45 @@ function gcd(a: number, b: number): number {
223231
}
224232
```
225233

234+
#### Rust
235+
236+
```rust
237+
impl Solution {
238+
pub fn min_operations(nums: Vec<i32>) -> i32 {
239+
let n = nums.len() as i32;
240+
let cnt = nums.iter().filter(|&&x| x == 1).count() as i32;
241+
if cnt > 0 {
242+
return n - cnt;
243+
}
244+
let mut mi = n + 1;
245+
for i in 0..nums.len() {
246+
let mut g = 0;
247+
for j in i..nums.len() {
248+
g = gcd(g, nums[j]);
249+
if g == 1 {
250+
mi = mi.min((j - i + 1) as i32);
251+
break;
252+
}
253+
}
254+
}
255+
if mi > n {
256+
-1
257+
} else {
258+
n - 1 + mi - 1
259+
}
260+
}
261+
}
262+
263+
fn gcd(mut a: i32, mut b: i32) -> i32 {
264+
while b != 0 {
265+
let tmp = a % b;
266+
a = b;
267+
b = tmp;
268+
}
269+
a.abs()
270+
}
271+
```
272+
226273
<!-- tabs:end -->
227274

228275
<!-- solution:end -->
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn min_operations(nums: Vec<i32>) -> i32 {
3+
let n = nums.len() as i32;
4+
let cnt = nums.iter().filter(|&&x| x == 1).count() as i32;
5+
if cnt > 0 {
6+
return n - cnt;
7+
}
8+
let mut mi = n + 1;
9+
for i in 0..nums.len() {
10+
let mut g = 0;
11+
for j in i..nums.len() {
12+
g = gcd(g, nums[j]);
13+
if g == 1 {
14+
mi = mi.min((j - i + 1) as i32);
15+
break;
16+
}
17+
}
18+
}
19+
if mi > n {
20+
-1
21+
} else {
22+
n - 1 + mi - 1
23+
}
24+
}
25+
}
26+
27+
fn gcd(mut a: i32, mut b: i32) -> i32 {
28+
while b != 0 {
29+
let tmp = a % b;
30+
a = b;
31+
b = tmp;
32+
}
33+
a.abs()
34+
}

0 commit comments

Comments
 (0)