-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1717: Maximum Score From Removing Substrings
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/problem_1717_maximum_score_from_removing_substrings/greedy.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/problem_1717_maximum_score_from_removing_substrings/greedy_2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/problem_1717_maximum_score_from_removing_substrings/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |