20240907 finished.
This commit is contained in:
parent
6360f90e9e
commit
8c2dfc5547
|
@ -228,3 +228,4 @@ 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;
|
||||
mod p3177_find_the_maximum_length_of_a_good_subsequence_ii;
|
|
@ -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<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_3177() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user