Find the Maximum Length of a Good Subsequence II - Problem

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

example_1.py โ€” Basic Case
$ Input: nums = [1,2,1,1,3], k = 2
โ€บ Output: 4
๐Ÿ’ก Note: The optimal subsequence is [1,2,1,1] with length 4. It has 2 transitions: 1โ†’2 and 2โ†’1, which equals our limit k=2.
example_2.py โ€” No Transitions Allowed
$ Input: nums = [1,2,3,4,5], k = 0
โ€บ Output: 1
๐Ÿ’ก Note: With k=0, no transitions are allowed, so we can only pick subsequences of identical elements. The maximum length is 1.
example_3.py โ€” All Same Elements
$ Input: nums = [5,5,5,5], k = 1
โ€บ Output: 4
๐Ÿ’ก Note: All elements are the same, so we can take the entire array with 0 transitions, well within our limit of k=1.

Visualization

Tap to expand
๐Ÿ”— Chain Building AnalogyBeads: [1, 2, 1, 1, 3] | Connectors Available: k = 21connector2connector1free1Length: 4, Connectors Used: 2 โœ“Dynamic Programming Statesdp[1][0] = 3 (three 1's with no transitions)dp[1][1] = 3 (can start with other color, then three 1's)dp[1][2] = 4 (optimal: 1โ†’2โ†’1โ†’1, using 2 connectors)dp[2][0] = 1 (one 2 with no transitions)dp[2][1] = 2 (can extend from existing chains)dp[3][2] = 4 (can transition from best 2-length chain)Algorithm Insightโ€ข Same color beads connect for freeโ€ข Different colors need connectorsโ€ข Track best chain for each color & connector countComplexity AnalysisTime: O(n ร— k ร— V) where V = unique valuesSpace: O(k ร— V) for DP tableMuch better than O(2โฟ) brute force!
Understanding the Visualization
1
Start Processing
Begin with first bead, creating initial chain segments
2
Same Color Extension
When we see the same color, extend existing chains for free
3
Color Transition
When colors differ, use a precious connector to join chains
4
Optimize Choices
Choose the best combination to maximize total chain length
Key Takeaway
๐ŸŽฏ Key Insight: By modeling this as a DP problem where we track the best subsequence ending with each value and transition count, we avoid the exponential complexity of checking all subsequences while finding the optimal solution efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * k * V)

n elements, k+1 transition states, V unique values in array

n
2n
โœ“ Linear Growth
Space Complexity
O(k * V)

DP table storing max length for each (value, transition_count) pair

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 5000
  • 1 โ‰ค nums[i] โ‰ค 103
  • 0 โ‰ค k โ‰ค min(nums.length - 1, 50)
  • Memory limit: 256 MB
  • Time limit: 2 seconds
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen