Skip to content

Commit

Permalink
Added maximum positive or negative integer count solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 28, 2024
1 parent e851811 commit 154029e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ other languages.
- Max Repeating Substring (https://leetcode.com/problems/maximum-repeating-substring/)
- Sort by Increasing Frequency (https://leetcode.com/problems/sort-array-by-increasing-frequency)
- Maximum Odd Binary Number (https://leetcode.com/problems/maximum-odd-binary-number)
- Maximum Count of Positive or Negative Integer (https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer)

#### Medium

Expand Down
25 changes: 25 additions & 0 deletions src/leetcode/max_positive_negative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::cmp::Ordering;

pub fn maximum_count(numbers: Vec<i32>) -> i32 {
let counts = numbers.into_iter().fold((0, 0), |mut acc, curr| {
match curr.cmp(&0) {
Ordering::Greater => acc.1 += 1,
Ordering::Less => acc.0 += 1,
Ordering::Equal => {}
}
acc
});
counts.0.max(counts.1)
}

#[cfg(test)]
mod tests {
use super::maximum_count;

#[test]
fn maximum_positive_negative_count_is_provided() {
let numbers = vec![-1, -2, 0, 2, 5, -3];
let actual = maximum_count(numbers);
assert_eq!(3, actual)
}
}
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ mod freq_sort;
mod sort_jumbled_numbers;
mod max_binary_number;
mod custom_sort;
mod max_positive_negative;

0 comments on commit 154029e

Please sign in to comment.