-
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 2155: All Divisions With the Highest Score of a Binary Array
- 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
47 changes: 47 additions & 0 deletions
47
src/problem_2155_all_divisions_with_the_highest_score_of_a_binary_array/iterative.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,47 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
|
||
impl Solution { | ||
pub fn max_score_indices(nums: Vec<i32>) -> Vec<i32> { | ||
let mut result = vec![0]; | ||
let mut max_score = 0; | ||
let mut score = 0; | ||
|
||
for (i, num) in (1..).zip(nums) { | ||
score += 1 - num * 2; | ||
|
||
match score.cmp(&max_score) { | ||
Ordering::Less => continue, | ||
Ordering::Equal => {} | ||
Ordering::Greater => { | ||
max_score = score; | ||
|
||
result.clear(); | ||
} | ||
} | ||
|
||
result.push(i); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_score_indices(nums: Vec<i32>) -> Vec<i32> { | ||
Self::max_score_indices(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/problem_2155_all_divisions_with_the_highest_score_of_a_binary_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,26 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn max_score_indices(nums: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[0, 0, 1, 0] as &[_], &[2, 4] as &[_]), | ||
(&[0, 0, 0], &[3]), | ||
(&[1, 1], &[0]), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!( | ||
test_utilities::unstable_sorted(S::max_score_indices(nums.to_vec())), | ||
expected, | ||
); | ||
} | ||
} | ||
} |