20250327 finished.

This commit is contained in:
jackfiled 2025-03-27 14:25:09 +08:00
parent 3d44342815
commit 7b042ef4be
2 changed files with 36 additions and 0 deletions

View File

@ -562,3 +562,5 @@ mod p2255_count_prefixes_of_a_given_string;
mod p2711_difference_of_number_of_distinct_values_on_diagonals;
mod p2829_determine_the_minimum_sum_of_a_k_avoiding_array;
mod p2712_minimum_cost_to_make_all_characters_equal;

View File

@ -0,0 +1,34 @@
/**
* [2712] Minimum Cost to Make All Characters Equal
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut result = 0;
let s: Vec<u8> = s.bytes().collect();
for i in 1..s.len() {
if s[i] != s[i - 1] {
result += i.min(s.len() - i) as i64;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2712() {
assert_eq!(2, Solution::minimum_cost("0011".to_owned()));
assert_eq!(9, Solution::minimum_cost("010101".to_owned()));
}
}