-
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 2148: Count Elements With Strictly Smaller and Greater El…
…ements
- Loading branch information
Showing
3 changed files
with
81 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
57 changes: 57 additions & 0 deletions
57
src/problem_2148_count_elements_with_strictly_smaller_and_greater_elements/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,57 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
|
||
impl Solution { | ||
pub fn count_elements(nums: Vec<i32>) -> i32 { | ||
let mut iter = nums.iter().copied(); | ||
let mut min = iter.next().unwrap(); | ||
let mut min_count = 1; | ||
let mut max = min; | ||
let mut max_count = 1; | ||
|
||
for num in iter { | ||
match num.cmp(&min) { | ||
Ordering::Less => { | ||
min = num; | ||
min_count = 1; | ||
} | ||
Ordering::Equal => min_count += 1, | ||
Ordering::Greater => {} | ||
} | ||
|
||
match num.cmp(&max) { | ||
Ordering::Less => {} | ||
Ordering::Equal => max_count += 1, | ||
Ordering::Greater => { | ||
max = num; | ||
max_count = 1; | ||
} | ||
} | ||
} | ||
|
||
if min == max { | ||
0 | ||
} else { | ||
nums.len() as i32 - min_count - max_count | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn count_elements(nums: Vec<i32>) -> i32 { | ||
Self::count_elements(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/problem_2148_count_elements_with_strictly_smaller_and_greater_elements/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,23 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn count_elements(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[11, 7, 2, 15] as &[_], 2), | ||
(&[-3, 3, 3, 90], 2), | ||
(&[0], 0), | ||
(&[-71, -71, 93, -71, 40], 1), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::count_elements(nums.to_vec()), expected); | ||
} | ||
} | ||
} |