You are given an integer array nums and a non-negative integer k. Your task is to find the maximum possible length of a "good" subsequence from the array.
A subsequence seq is considered good if there are at most k positions where consecutive elements are different. More formally, there should be at most k indices i in the range [0, seq.length - 2] such that seq[i] != seq[i + 1].
Example: For array [1,2,1,1,3] with k=2, the subsequence [1,2,1,3] has 3 transitions (1โ2, 2โ1, 1โ3), but we can only have 2, so we might choose [1,1,1] with length 3 and 0 transitions instead.
๐ฏ Goal: Return the maximum length of such a good subsequence.
Input & Output
Visualization
Time & Space Complexity
n elements, k+1 transition states, V unique values in array
DP table storing max length for each (value, transition_count) pair
Constraints
- 1 โค nums.length โค 5000
- 1 โค nums[i] โค 103
- 0 โค k โค min(nums.length - 1, 50)
- Memory limit: 256 MB
- Time limit: 2 seconds