-
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 2369: Check if There is a Valid Partition For The Array
- Loading branch information
Showing
3 changed files
with
59 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
40 changes: 40 additions & 0 deletions
40
src/problem_2369_check_if_there_is_a_valid_partition_for_the_array/dynamic_programming.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,40 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn valid_partition(nums: Vec<i32>) -> bool { | ||
let mut cache = (false, false, true); | ||
let mut prev = (0, 0); | ||
|
||
for num in nums { | ||
cache = ( | ||
cache.1, | ||
cache.2, | ||
(cache.1 && prev.1 == num) | ||
|| (cache.0 | ||
&& ((prev.0 == prev.1 && prev.1 == num) || (prev.0 + 1 == prev.1 && prev.1 + 1 == num))), | ||
); | ||
|
||
prev = (prev.1, num); | ||
} | ||
|
||
cache.2 | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn valid_partition(nums: Vec<i32>) -> bool { | ||
Self::valid_partition(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_2369_check_if_there_is_a_valid_partition_for_the_array/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 dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn valid_partition(nums: Vec<i32>) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(&[4, 4, 4, 5, 6] as &[_], true), (&[1, 1, 1, 2], false)]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::valid_partition(nums.to_vec()), expected); | ||
} | ||
} | ||
} |