From 566c6a8414ab40d81d453707d10c552b29dad9d4 Mon Sep 17 00:00:00 2001 From: li_chx Date: Thu, 5 Mar 2026 08:45:56 +0800 Subject: [PATCH] =?UTF-8?q?1758.=20=E7=94=9F=E6=88=90=E4=BA=A4=E6=9B=BF?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=93=8D=E4=BD=9C=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 60 +++++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ce79f9..7e0aab5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,51 +1,33 @@ +use std::cmp::min; + struct Solution; mod arr; impl Solution { - pub fn num_special(mat: Vec>) -> i32 { - let mut disable = vec![false; mat[0].len()]; - let mut arr = vec![0; mat[0].len()]; - for i in 0..mat.len() { - let mut disable_i = false; - let mut next_do_disable = false; - let mut last_index = 1000_usize; - for j in 0..mat[0].len() { - if mat[i][j] == 1 { - if next_do_disable { - disable_i = true; - arr[last_index] = 0; - disable[last_index] = true; - disable[j] = true; - arr[j] = 0; - } - if disable[j] { - disable_i = true; - continue; - } - if disable_i { - disable[j] = true; - if arr[j] != 0 { - arr[j] = 0; - } - continue; - } - if arr[j] != 0 { - disable[j] = true; - arr[j] = 0; - disable_i = true; - continue - } - arr[j] += 1; - next_do_disable = true; - last_index = j; + pub fn min_operations(s: String) -> i32 { + let (mut cnt_a, mut cnt_b) = (0, 0); + let mut cnt = 0; + for c in s.chars() { + if cnt & 1 == 1 { + if c == '0' { + cnt_a += 1; + } else { + cnt_b += 1; + } + } else { + if c == '0' { + cnt_b += 1; + } else { + cnt_a += 1; } } + cnt += 1; } - arr.iter().sum() + min(cnt_a, cnt_b) } } fn main() { - let result = Solution::num_special(arr::make_matrix("[[0,0,0,0,0,0],[0,0,1,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,0],[0,0,1,0,0,1],[0,0,0,0,0,0],[0,0,0,0,0,0]]")); + let result = Solution::min_operations("1111".to_string()); println!("{:?}", result); } // [[0,0,0,0,0,0], @@ -54,4 +36,4 @@ fn main() { // [0,0,0,0,0,0], // [0,0,1,0,0,1], // [0,0,0,0,0,0], -// [0,0,0,0,0,0]] \ No newline at end of file +// [0,0,0,0,0,0]]