-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1365: How Many Numbers Are Smaller Than the Current Number
- Loading branch information
Showing
3 changed files
with
63 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_1365_how_many_numbers_are_smaller_than_the_current_number/heap.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; | ||
|
||
use std::{ | ||
cmp::Reverse, | ||
collections::{BinaryHeap, HashMap}, | ||
}; | ||
|
||
impl Solution { | ||
pub fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32> { | ||
let mut result = vec![0; nums.len()]; | ||
let mut map = HashMap::with_capacity(nums.len()); | ||
let mut heap: BinaryHeap<Reverse<_>> = | ||
nums.into_iter().zip(0usize..).map(Reverse).collect(); | ||
|
||
let mut count = 0; | ||
while let Some(Reverse((val, idx))) = heap.pop() { | ||
if let Some(x) = map.insert(val, idx) { | ||
result[idx] = result[x]; | ||
} else { | ||
result[idx] = count; | ||
} | ||
count += 1; | ||
} | ||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32> { | ||
Self::smaller_numbers_than_current(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1365_how_many_numbers_are_smaller_than_the_current_number/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,22 @@ | ||
pub mod heap; | ||
|
||
pub trait Solution { | ||
fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[8, 1, 2, 2, 3] as &[_], &[4, 0, 1, 1, 3] as &[_]), | ||
(&[6, 5, 4, 8], &[2, 1, 0, 3]), | ||
(&[7, 7, 7, 7], &[0, 0, 0, 0]), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::smaller_numbers_than_current(nums.to_vec()), expected); | ||
} | ||
} | ||
} |