-
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 1713: Minimum Operations to Make a Subsequence
- Loading branch information
Showing
3 changed files
with
69 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
47 changes: 47 additions & 0 deletions
47
src/problem_1713_minimum_operations_to_make_a_subsequence/longest_increasing_subsequence.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,47 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn min_operations(target: Vec<i32>, arr: Vec<i32>) -> i32 { | ||
let ranks = (0_u32..) | ||
.zip(target) | ||
.map(|(i, num)| (num, i)) | ||
.collect::<HashMap<_, _>>(); | ||
|
||
let mut length_to_min_index = Vec::with_capacity(ranks.len()); | ||
|
||
for target in arr { | ||
if let Some(&rank) = ranks.get(&target) { | ||
let length = length_to_min_index.partition_point(|&left_index| left_index < rank); | ||
|
||
#[allow(clippy::option_if_let_else)] // False positive. | ||
if let Some(min_rank) = length_to_min_index.get_mut(length) { | ||
*min_rank = rank; | ||
} else { | ||
length_to_min_index.push(rank); | ||
} | ||
} | ||
} | ||
|
||
(ranks.len() - length_to_min_index.len()) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_operations(target: Vec<i32>, arr: Vec<i32>) -> i32 { | ||
Self::min_operations(target, arr) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/problem_1713_minimum_operations_to_make_a_subsequence/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 longest_increasing_subsequence; | ||
|
||
pub trait Solution { | ||
fn min_operations(target: Vec<i32>, arr: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[5, 1, 3] as &[_], &[9, 4, 2, 3, 4] as &[_]), 2), | ||
((&[6, 4, 8, 1, 3, 2], &[4, 7, 6, 2, 3, 8, 6, 1]), 3), | ||
]; | ||
|
||
for ((target, arr), expected) in test_cases { | ||
assert_eq!(S::min_operations(target.to_vec(), arr.to_vec()), expected); | ||
} | ||
} | ||
} |