Skip to content

Commit 1901f44

Browse files
committed
feat: add rust soluiton to lc problem: No.2436
1 parent d026c37 commit 1901f44

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,33 @@ function gcd(a: number, b: number): number {
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn minimum_splits(nums: Vec<i32>) -> i32 {
195+
let mut ans = 1;
196+
let mut g = 0;
197+
for &x in &nums {
198+
g = Self::gcd(g, x);
199+
if g == 1 {
200+
ans += 1;
201+
g = x;
202+
}
203+
}
204+
ans
205+
}
206+
207+
fn gcd(a: i32, b: i32) -> i32 {
208+
if b == 0 {
209+
a
210+
} else {
211+
Self::gcd(b, a % b)
212+
}
213+
}
214+
}
215+
```
216+
190217
<!-- tabs:end -->
191218

192219
<!-- solution:end -->

solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,33 @@ function gcd(a: number, b: number): number {
183183
}
184184
```
185185

186+
#### Rust
187+
188+
```rust
189+
impl Solution {
190+
pub fn minimum_splits(nums: Vec<i32>) -> i32 {
191+
let mut ans = 1;
192+
let mut g = 0;
193+
for &x in &nums {
194+
g = Self::gcd(g, x);
195+
if g == 1 {
196+
ans += 1;
197+
g = x;
198+
}
199+
}
200+
ans
201+
}
202+
203+
fn gcd(a: i32, b: i32) -> i32 {
204+
if b == 0 {
205+
a
206+
} else {
207+
Self::gcd(b, a % b)
208+
}
209+
}
210+
}
211+
```
212+
186213
<!-- tabs:end -->
187214

188215
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn minimum_splits(nums: Vec<i32>) -> i32 {
3+
let mut ans = 1;
4+
let mut g = 0;
5+
for &x in &nums {
6+
g = Self::gcd(g, x);
7+
if g == 1 {
8+
ans += 1;
9+
g = x;
10+
}
11+
}
12+
ans
13+
}
14+
15+
fn gcd(a: i32, b: i32) -> i32 {
16+
if b == 0 {
17+
a
18+
} else {
19+
Self::gcd(b, a % b)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)