-
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 1409: Queries on a Permutation With Key
- Loading branch information
Showing
3 changed files
with
51 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,22 @@ | ||
pub mod rotate; | ||
|
||
pub trait Solution { | ||
fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[3, 1, 2, 1] as &[_], 5), &[2, 1, 2, 1] as &[_]), | ||
((&[4, 1, 2, 2], 4), &[3, 1, 2, 0]), | ||
((&[7, 5, 5, 8, 3], 8), &[6, 5, 0, 7, 5]), | ||
]; | ||
|
||
for ((queries, m), expected) in test_cases { | ||
assert_eq!(S::process_queries(queries.to_vec(), m), expected); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/problem_1409_queries_on_a_permutation_with_key/rotate.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,28 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32> { | ||
let mut result = Vec::with_capacity(queries.len()); | ||
let mut nums: Vec<i32> = (1..=m).collect(); | ||
for query in queries { | ||
let pos = nums.iter().position(|&x| x == query).unwrap(); | ||
result.push(pos as i32); | ||
nums[0..=pos].rotate_right(1); | ||
} | ||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32> { | ||
Self::process_queries(queries, m) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |