Skip to content

Commit

Permalink
Add problem 2155: All Divisions With the Highest Score of a Binary Array
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Mar 1, 2024
1 parent 7eebd59 commit ef649da
Show file tree
Hide file tree
Showing 3 changed files with 74 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 @@ -1464,6 +1464,7 @@ pub mod problem_2148_count_elements_with_strictly_smaller_and_greater_elements;
pub mod problem_2149_rearrange_array_elements_by_sign;
pub mod problem_2150_find_all_lonely_numbers_in_the_array;
pub mod problem_2154_keep_multiplying_found_values_by_two;
pub mod problem_2155_all_divisions_with_the_highest_score_of_a_binary_array;

#[cfg(test)]
mod test_utilities;
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>();
}
}
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,
);
}
}
}

0 comments on commit ef649da

Please sign in to comment.