20250317 finished.
This commit is contained in:
parent
29372e18ab
commit
8285dd6654
|
@ -542,3 +542,5 @@ mod p3340_check_balanced_string;
|
||||||
mod p3110_score_of_a_string;
|
mod p3110_score_of_a_string;
|
||||||
|
|
||||||
mod p2272_substring_with_largest_variance;
|
mod p2272_substring_with_largest_variance;
|
||||||
|
|
||||||
|
mod p1963_minimum_number_of_swaps_to_make_the_string_balanced;
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
/**
|
||||||
|
* [1963] Minimum Number of Swaps to Make the String Balanced
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn min_swaps(s: String) -> i32 {
|
||||||
|
let mut count = 0;
|
||||||
|
let mut min_count = 0;
|
||||||
|
|
||||||
|
for c in s.chars() {
|
||||||
|
match c {
|
||||||
|
'[' => count += 1,
|
||||||
|
']' => {
|
||||||
|
count -= 1;
|
||||||
|
min_count = min_count.min(count)
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(-min_count + 1) / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1963() {
|
||||||
|
assert_eq!(1, Solution::min_swaps("][][".to_owned()));
|
||||||
|
assert_eq!(2, Solution::min_swaps("]]][[[".to_owned()));
|
||||||
|
assert_eq!(0, Solution::min_swaps("[]".to_owned()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user