diff --git a/src/lib.rs b/src/lib.rs index 1535c1fc..1844c6d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1385,6 +1385,7 @@ pub mod problem_1710_maximum_units_on_a_truck; pub mod problem_1711_count_good_meals; pub mod problem_1712_ways_to_split_array_into_three_subarrays; pub mod problem_1716_calculate_money_in_leetcode_bank; +pub mod problem_1717_maximum_score_from_removing_substrings; pub mod problem_1720_decode_xored_array; pub mod problem_1721_swapping_nodes_in_a_linked_list; pub mod problem_1725_number_of_rectangles_that_can_form_the_largest_square; diff --git a/src/problem_1717_maximum_score_from_removing_substrings/greedy.rs b/src/problem_1717_maximum_score_from_removing_substrings/greedy.rs new file mode 100644 index 00000000..0ae57651 --- /dev/null +++ b/src/problem_1717_maximum_score_from_removing_substrings/greedy.rs @@ -0,0 +1,49 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 { + let (first, second, x, y) = if y < x { (b'a', b'b', x, y) } else { (b'b', b'a', y, x) }; + let mut num_first = 0; + let mut num_second = 0; + let mut result = 0; + + for c in s.into_bytes() { + if c == first { + num_first += 1; + } else if c == second { + if num_first == 0 { + num_second += 1; + } else { + num_first -= 1; + result += x; + } + } else { + result += y * num_second.min(num_first); + num_first = 0; + num_second = 0; + } + } + + result += y * num_second.min(num_first); + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn maximum_gain(s: String, x: i32, y: i32) -> i32 { + Self::maximum_gain(s, x, y) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1717_maximum_score_from_removing_substrings/greedy_2.rs b/src/problem_1717_maximum_score_from_removing_substrings/greedy_2.rs new file mode 100644 index 00000000..2a3a7e54 --- /dev/null +++ b/src/problem_1717_maximum_score_from_removing_substrings/greedy_2.rs @@ -0,0 +1,56 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn check(s: &str, x: i32, y: i32) -> i32 { + let mut num_first = 0; + let mut num_second = 0; + let mut result = 0; + + for c in s.bytes() { + if c == A { + num_first += 1; + } else if c == B { + if num_first == 0 { + num_second += 1; + } else { + num_first -= 1; + result += x; + } + } else { + result += y * num_second.min(num_first); + num_first = 0; + num_second = 0; + } + } + + result += y * num_second.min(num_first); + + result + } + + pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 { + if y < x { + Self::check::(&s, x, y) + } else { + Self::check::(&s, y, x) + } + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn maximum_gain(s: String, x: i32, y: i32) -> i32 { + Self::maximum_gain(s, x, y) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1717_maximum_score_from_removing_substrings/mod.rs b/src/problem_1717_maximum_score_from_removing_substrings/mod.rs new file mode 100644 index 00000000..854ad2bd --- /dev/null +++ b/src/problem_1717_maximum_score_from_removing_substrings/mod.rs @@ -0,0 +1,19 @@ +pub mod greedy; +pub mod greedy_2; + +pub trait Solution { + fn maximum_gain(s: String, x: i32, y: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [(("cdbcbbaaabab", 4, 5), 19), (("aabbaaxybbaabb", 5, 4), 20)]; + + for ((s, x, y), expected) in test_cases { + assert_eq!(S::maximum_gain(s.to_string(), x, y), expected); + } + } +}