3499. 操作后最大活跃区段数 I

This commit is contained in:
li-chx 2026-07-28 18:35:53 +08:00
parent fc8857537e
commit 86ec2ee252
No known key found for this signature in database
GPG Key ID: 9EC03532D8AB7639
1 changed files with 20 additions and 22 deletions

View File

@ -1,32 +1,30 @@
mod arr; mod arr;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn smallest_palindrome(s: String) -> String { pub fn max_active_sections_after_trade(s: String) -> i32 {
let mut arr = vec![0; 26]; let (mut last_zero_count, mut zero_count) = (0,0);
let mut s = s.chars().collect::<Vec<char>>(); let mut ans = 0;
for i in 0..s.len() / 2 { let mut max_p = 0;
arr[(s[i] as u8 - b'a') as usize] += 1; 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);
} }
let mut idx = 0; last_zero_count = zero_count;
for i in 0..26 { zero_count = 0
while arr[i] > 0 {
s[idx] = (b'a' + i as u8) as char;
arr[i] -= 1;
idx += 1;
} }
} }
let mut rev_idx = idx;
if s.len() % 2 != 0{
idx += 1;
} }
while rev_idx > 0{ if zero_count > 0 && last_zero_count > 0 {
rev_idx -= 1; max_p = max_p.max(zero_count + last_zero_count);
s[idx] = s[rev_idx];
idx += 1;
} }
s.into_iter().collect::<String>() ans + max_p
} }
} }
fn main() { fn main() {
println!("{:?}", Solution::smallest_palindrome("bahaahab".to_string())); println!("{:?}", Solution::max_active_sections_after_trade("0100".to_string()));
} }