20240906 finished.

This commit is contained in:
jackfiled 2024-09-06 15:57:51 +08:00
parent 49a4df064a
commit 6360f90e9e
2 changed files with 43 additions and 1 deletions

View File

@ -227,3 +227,4 @@ mod p2024_maximize_the_confusion_of_an_exam;
mod p2708_maximum_strength_of_a_group;
mod p2860_happy_students;
mod p3174_clear_digits;
mod p3176_find_the_maximum_length_of_a_good_subsequence_i;

View File

@ -0,0 +1,41 @@
/**
* [3176] Find the Maximum Length of a Good Subsequence I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn maximum_length(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let k = k as usize;
let mut map = HashMap::with_capacity(nums.len());
let mut mx = vec![0; k + 2];
for x in nums {
let entry = map.entry(x).or_insert(vec![0; k + 1]);
for j in (0..=k).rev() {
entry[j] = entry[j].max(mx[j]) + 1;
mx[j + 1] = mx[j + 1].max(entry[j]);
}
}
mx[k + 1]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3176() {
assert_eq!(4, Solution::maximum_length(vec![1, 2, 1, 1, 3], 2));
assert_eq!(2, Solution::maximum_length(vec![1,2,3,4,5,1], 0));
}
}