diff --git a/src/problem/mod.rs b/src/problem/mod.rs index de09f06..7fdc7f2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -227,4 +227,5 @@ 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; \ No newline at end of file +mod p3176_find_the_maximum_length_of_a_good_subsequence_i; +mod p3177_find_the_maximum_length_of_a_good_subsequence_ii; \ No newline at end of file diff --git a/src/problem/p3177_find_the_maximum_length_of_a_good_subsequence_ii.rs b/src/problem/p3177_find_the_maximum_length_of_a_good_subsequence_ii.rs new file mode 100644 index 0000000..6b6c77f --- /dev/null +++ b/src/problem/p3177_find_the_maximum_length_of_a_good_subsequence_ii.rs @@ -0,0 +1,39 @@ +/** + * [3177] Find the Maximum Length of a Good Subsequence II + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn maximum_length(nums: Vec, 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_3177() { + } +}