Skip to content

Practice Find the Maximum Length of a Good Subsequence II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3177
LeetCode Problem

Find the Maximum Length of a Good Subsequence II

Problem Statement

You are given an integer array nums and a non-negative integer k. A sequence of integers seq is called good if there are at most k indices i in the range [0, seq.length - 2] such that seq[i] != seq[i + 1]. Return the maximum possible length of a good subsequence of nums.

Source: Find the Maximum Length of a Good Subsequence II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,1,1,3], k = 2
Output
4

# Example 2

Input
nums = [1,2,3,4,5,1], k = 0
Output
2

Constraints

1 <= nums.length <= 5 * 10^3
1 <= nums[i] <= 10^9
0 <= k <= min(50, nums.length)

Related LeetCode Problems

Practice more problems similar to Find the Maximum Length of a Good Subsequence II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.