From 1901f44b8967c0d6152e285f00c1c25f201829b9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 26 Nov 2025 07:13:13 +0800 Subject: [PATCH] feat: add rust soluiton to lc problem: No.2436 --- .../README.md | 27 +++++++++++++++++++ .../README_EN.md | 27 +++++++++++++++++++ .../Solution.rs | 22 +++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md index 5e41c1d04f2dd..34c62dfbd1779 100644 --- a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README.md @@ -187,6 +187,33 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +} +``` + diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md index 265626b0a1d8d..7932dffee9b58 100644 --- a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/README_EN.md @@ -183,6 +183,33 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +} +``` + diff --git a/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs new file mode 100644 index 0000000000000..83319cebeb72a --- /dev/null +++ b/solution/2400-2499/2436.Minimum Split Into Subarrays With GCD Greater Than One/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn minimum_splits(nums: Vec) -> i32 { + let mut ans = 1; + let mut g = 0; + for &x in &nums { + g = Self::gcd(g, x); + if g == 1 { + ans += 1; + g = x; + } + } + ans + } + + fn gcd(a: i32, b: i32) -> i32 { + if b == 0 { + a + } else { + Self::gcd(b, a % b) + } + } +}