-
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 1896: Minimum Cost to Change the Final Value of Expression
- Loading branch information
Showing
3 changed files
with
74 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
18 changes: 18 additions & 0 deletions
18
src/problem_1896_minimum_cost_to_change_the_final_value_of_expression/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,18 @@ | ||
pub mod recursive; | ||
|
||
pub trait Solution { | ||
fn min_operations_to_flip(expression: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [("1&(0|1)", 1), ("(0&0)&(0&0&0)", 3), ("(0|(1|0&1))", 1)]; | ||
|
||
for (expression, expected) in test_cases { | ||
assert_eq!(S::min_operations_to_flip(expression.to_string()), expected); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/problem_1896_minimum_cost_to_change_the_final_value_of_expression/recursive.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,55 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::str::Bytes; | ||
|
||
impl Solution { | ||
fn parse_term(iter: &mut Bytes) -> (bool, u32) { | ||
match iter.next().unwrap() { | ||
b'(' => Self::parse_expression(iter), | ||
c => (c != b'0', 1), | ||
} | ||
} | ||
|
||
fn parse_expression(iter: &mut Bytes) -> (bool, u32) { | ||
let mut lhs = Self::parse_term(iter); | ||
|
||
while let Some(operator) = iter.next() { | ||
if operator == b')' { | ||
break; | ||
} | ||
|
||
let rhs = Self::parse_term(iter); | ||
let is_or = operator == b'|'; | ||
|
||
lhs = match (lhs.0 == is_or, rhs.0 == is_or) { | ||
(false, false) => (!is_or, lhs.1.min(rhs.1)), | ||
(true, false) | (false, true) => (is_or, 1), | ||
(true, true) => (is_or, lhs.1.min(rhs.1) + 1), | ||
}; | ||
} | ||
|
||
lhs | ||
} | ||
|
||
pub fn min_operations_to_flip(expression: String) -> i32 { | ||
Self::parse_expression(&mut expression.bytes()).1 as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_operations_to_flip(expression: String) -> i32 { | ||
Self::min_operations_to_flip(expression) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |