From 86ec2ee252a97214c6286030dfded2bb3413cce6 Mon Sep 17 00:00:00 2001 From: li-chx Date: Tue, 28 Jul 2026 18:35:53 +0800 Subject: [PATCH] =?UTF-8?q?3499.=20=E6=93=8D=E4=BD=9C=E5=90=8E=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=B4=BB=E8=B7=83=E5=8C=BA=E6=AE=B5=E6=95=B0=20I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e6cb60..137ad18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,32 +1,30 @@ mod arr; struct Solution {} impl Solution { - pub fn smallest_palindrome(s: String) -> String { - let mut arr = vec![0; 26]; - let mut s = s.chars().collect::>(); - for i in 0..s.len() / 2 { - arr[(s[i] as u8 - b'a') as usize] += 1; - } - let mut idx = 0; - for i in 0..26 { - while arr[i] > 0 { - s[idx] = (b'a' + i as u8) as char; - arr[i] -= 1; - idx += 1; + pub fn max_active_sections_after_trade(s: String) -> i32 { + let (mut last_zero_count, mut zero_count) = (0,0); + let mut ans = 0; + let mut max_p = 0; + for c in s.chars() { + if c == '0' { + zero_count += 1; + }else { + ans += 1; + if zero_count > 0{ + if last_zero_count > 0 { + max_p = max_p.max(last_zero_count + zero_count); + } + last_zero_count = zero_count; + zero_count = 0 + } } } - let mut rev_idx = idx; - if s.len() % 2 != 0{ - idx += 1; + if zero_count > 0 && last_zero_count > 0 { + max_p = max_p.max(zero_count + last_zero_count); } - while rev_idx > 0{ - rev_idx -= 1; - s[idx] = s[rev_idx]; - idx += 1; - } - s.into_iter().collect::() + ans + max_p } } fn main() { - println!("{:?}", Solution::smallest_palindrome("bahaahab".to_string())); + println!("{:?}", Solution::max_active_sections_after_trade("0100".to_string())); }