Find the Maximum Length of a Good Subsequence I

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.

Example 1
Inputnums = [1,2,1,1,3], k = 2
Output4
The maximum length subsequence is [1, 2, 1, 1, 3].
Example 2
Inputnums = [1,2,3,4,5,1], k = 0
Output2
The maximum length subsequence is [1, 2, 3, 4, 5, 1].

Constraints

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 10^9
  • 0 <= k <= min(nums.length, 25)

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate