Skip to content

Commit

Permalink
Add problem 2111: Minimum Operations to Make the Array K-Increasing
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Aug 29, 2024
1 parent 27b0e24 commit 2e01822
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,7 @@ pub mod problem_2105_watering_plants_ii;
pub mod problem_2108_find_first_palindromic_string_in_the_array;
pub mod problem_2109_adding_spaces_to_a_string;
pub mod problem_2110_number_of_smooth_descent_periods_of_a_stock;
pub mod problem_2111_minimum_operations_to_make_the_array_k_increasing;
pub mod problem_2114_maximum_number_of_words_found_in_sentences;
pub mod problem_2115_find_all_possible_recipes_from_given_supplies;
pub mod problem_2116_check_if_a_parentheses_string_can_be_valid;
Expand Down
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>();
}
}
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>();
}
}
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);
}
}
}

0 comments on commit 2e01822

Please sign in to comment.