From 9648737245ba7cb00a8b98e5780dbe7262e52957 Mon Sep 17 00:00:00 2001 From: li-chx Date: Fri, 21 Nov 2025 09:55:06 +0800 Subject: [PATCH] =?UTF-8?q?2654.=20=E4=BD=BF=E6=95=B0=E7=BB=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=85=83=E7=B4=A0=E5=8F=98=E6=88=90=201=20=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=93=8D=E4=BD=9C=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5081428..fe47d2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,39 @@ struct Solution; + impl Solution { - pub fn max_operations(s: String) -> i32 { - let s = s.as_bytes(); - let mut ans = 0; - let mut ones = 0; - let mut first_zero = false; - for i in 0..s.len() { - if s[i] ==b'1' { + fn gcd_euclid(mut a: i32, mut b: i32) -> i32 { + while b != 0 { + let r = a % b; + a = b; + b = r; + } + a + } + pub fn min_operations(nums: Vec) -> i32 { + let mut ones = 0; + for i in 0..nums.len() { + if nums[i] == 1 { ones += 1; - first_zero = true; } - else { - if first_zero { - ans += ones; - first_zero = false; + } + if ones > 0 { + return (nums.len() - ones) as i32; + } + for i in 2..=nums.len() { + for j in 0..nums.len() - i + 1 { + let mut gcd = nums[j]; + for k in j + 1..j + i { + gcd = Self::gcd_euclid(gcd, nums[k]); + } + if gcd == 1 { + return nums.len() as i32 - 1 + i as i32 - 1; } } } - ans + -1 } } fn main() { - let result = Solution::max_operations(String::from("1001101")); + let result = Solution::min_operations(vec![1,1]); println!("{:?}", result); }