Skip to content

Commit

Permalink
Add problem 1717: Maximum Score From Removing Substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 14, 2023
1 parent 9e9f915 commit 5226228
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions src/problem_1717_maximum_score_from_removing_substrings/greedy.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
fn check<const A: u8, const B: u8>(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::<b'a', b'b'>(&s, x, y)
} else {
Self::check::<b'b', b'a'>(&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::<super::Solution>();
}
}
19 changes: 19 additions & 0 deletions src/problem_1717_maximum_score_from_removing_substrings/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit 5226228

Please sign in to comment.