-
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 2111: Minimum Operations to Make the Array K-Increasing
- Loading branch information
Showing
4 changed files
with
130 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
53 changes: 53 additions & 0 deletions
53
..._2111_minimum_operations_to_make_the_array_k_increasing/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,53 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroUsize; | ||
|
||
impl Solution { | ||
pub fn k_increasing(arr: Vec<i32>, k: i32) -> i32 { | ||
let k = NonZeroUsize::new(k as u32 as _).unwrap(); | ||
let mut lengths = Vec::new(); | ||
let mut iter = arr.iter(); | ||
let mut result = 0; | ||
|
||
for _ in 0..k.get() { | ||
let iter_2 = iter.clone().step_by(k.get()); | ||
|
||
result += iter_2.len() as u32; | ||
|
||
for &num in iter_2 { | ||
let num = num as u32; | ||
let i = lengths.partition_point(|&x| x <= num); | ||
|
||
if let Some(value) = lengths.get_mut(i) { | ||
*value = num; | ||
} else { | ||
lengths.push(num); | ||
} | ||
} | ||
|
||
result -= lengths.len() as u32; | ||
lengths.clear(); | ||
iter.next(); | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn k_increasing(arr: Vec<i32>, k: i32) -> i32 { | ||
Self::k_increasing(arr, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...111_minimum_operations_to_make_the_array_k_increasing/longest_increasing_subsequence_2.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,52 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroUsize; | ||
|
||
impl Solution { | ||
pub fn k_increasing(arr: Vec<i32>, k: i32) -> i32 { | ||
let k = NonZeroUsize::new(k as u32 as _).unwrap(); | ||
let mut lengths = Vec::new(); | ||
let mut result = 0; | ||
|
||
for start in 0..k.get() { | ||
result += ((arr.len() - start - 1) / k + 1) as u32; | ||
|
||
let mut i = start; | ||
|
||
while let Some(&num) = arr.get(i) { | ||
i += k.get(); | ||
|
||
let tail = lengths.partition_point(|&x| x <= num); | ||
|
||
if let Some(value) = lengths.get_mut(tail) { | ||
*value = num; | ||
} else { | ||
lengths.push(num); | ||
} | ||
} | ||
|
||
result -= lengths.len() as u32; | ||
lengths.clear(); | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn k_increasing(arr: Vec<i32>, k: i32) -> i32 { | ||
Self::k_increasing(arr, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/problem_2111_minimum_operations_to_make_the_array_k_increasing/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,24 @@ | ||
pub mod longest_increasing_subsequence; | ||
pub mod longest_increasing_subsequence_2; | ||
|
||
pub trait Solution { | ||
fn k_increasing(arr: Vec<i32>, k: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[5, 4, 3, 2, 1] as &[_], 1), 4), | ||
((&[4, 1, 5, 2, 6, 2], 2), 0), | ||
((&[4, 1, 5, 2, 6, 2], 3), 2), | ||
((&[12, 6, 12, 6, 14, 2, 13, 17, 3, 8, 11, 7, 4, 11, 18, 8, 8, 3], 1), 12), | ||
]; | ||
|
||
for ((arr, k), expected) in test_cases { | ||
assert_eq!(S::k_increasing(arr.to_vec(), k), expected); | ||
} | ||
} | ||
} |