-
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 2121: Intervals Between Identical Elements
- Loading branch information
Showing
3 changed files
with
86 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
64 changes: 64 additions & 0 deletions
64
src/problem_2121_intervals_between_identical_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,64 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::ptr; | ||
|
||
impl Solution { | ||
fn address(value: &i64) -> usize { | ||
ptr::from_ref(value) as _ | ||
} | ||
|
||
pub fn get_distances(arr: Vec<i32>) -> Vec<i64> { | ||
let mut result = vec![0; arr.len()]; | ||
let mut indices = HashMap::<_, Vec<_>>::new(); | ||
|
||
for (num, target) in arr.into_iter().zip(&mut result) { | ||
match indices.entry(num) { | ||
Entry::Occupied(entry) => entry.into_mut().push(target), | ||
Entry::Vacant(entry) => { | ||
entry.insert(vec![target]); | ||
} | ||
} | ||
} | ||
|
||
for targets in indices.values_mut() { | ||
let mut sum = targets | ||
.iter() | ||
.fold(0_u64, |sum, index| sum + Self::address(index) as u64); | ||
|
||
let mut count_diff = (targets.len() as u64).wrapping_neg(); | ||
let mut prev = 0; | ||
|
||
for target in targets { | ||
let address = Self::address(target) as u64; | ||
|
||
sum = sum.wrapping_add((address - prev).wrapping_mul(count_diff)); | ||
count_diff = count_diff.wrapping_add(2); | ||
prev = address; | ||
|
||
**target = (sum / (u64::from(usize::BITS) / 8)) as _; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn get_distances(arr: Vec<i32>) -> Vec<i64> { | ||
Self::get_distances(arr) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/problem_2121_intervals_between_identical_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,21 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn get_distances(arr: Vec<i32>) -> Vec<i64>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[2, 1, 3, 1, 2, 3, 3] as &[_], &[4_i64, 2, 7, 2, 4, 4, 5] as &[_]), | ||
(&[10, 5, 10, 10], &[5, 0, 3, 4]), | ||
]; | ||
|
||
for (arr, expected) in test_cases { | ||
assert_eq!(S::get_distances(arr.to_vec()), expected); | ||
} | ||
} | ||
} |