-
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 2295: Replace Elements in an Array
- Loading branch information
Showing
3 changed files
with
74 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
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,46 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn array_change(nums: Vec<i32>, operations: Vec<Vec<i32>>) -> Vec<i32> { | ||
let mut nums = nums; | ||
let mut operations = operations; | ||
let mut replacements = HashMap::new(); | ||
|
||
while let Some(operation) = operations.pop() { | ||
let [from, to]: [_; 2] = operation.try_into().ok().unwrap(); | ||
let to = replacements.get(&to).copied().unwrap_or(to); | ||
|
||
replacements.insert(from, to); | ||
} | ||
|
||
drop(operations); | ||
|
||
for num in &mut nums { | ||
if let Some(replacement) = replacements.get(num) { | ||
*num = *replacement; | ||
} | ||
} | ||
|
||
nums | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn array_change(nums: Vec<i32>, operations: Vec<Vec<i32>>) -> Vec<i32> { | ||
Self::array_change(nums, operations) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,27 @@ | ||
pub mod hash_map; | ||
|
||
pub trait Solution { | ||
fn array_change(nums: Vec<i32>, operations: Vec<Vec<i32>>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
(&[1, 2, 4, 6] as &[_], &[[1, 3], [4, 7], [6, 1]] as &[_]), | ||
&[3, 2, 7, 1] as &[_], | ||
), | ||
((&[1, 2], &[[1, 3], [2, 1], [3, 2]]), &[2, 1]), | ||
]; | ||
|
||
for ((nums, operations), expected) in test_cases { | ||
assert_eq!( | ||
S::array_change(nums.to_vec(), operations.iter().map(Vec::from).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |