Ctrl K

Playlist With Few Genre Changes

Medium
You have a queue of tracks, each tagged with a genre number: the array genres. You build a playlist by striking out any tracks you like from the queue; the rest keep their original order. A genre change is a pair of neighbouring tracks in the playlist with different genres. The listener tolerates at most switches changes. Return the largest possible number of tracks in such a playlist.
Example 1
genres=[4, 4, 7, 4, 7, 7], switches=1 → 5

Take 4, 4, 4, 7, 7 (skipping the first 7): one genre change. All six tracks would mean three changes.

Example 2
genres=[3, 8, 3, 8], switches=0 → 2

With no changes allowed, only tracks of one genre fit: the two 3s or the two 8s.

Example 3
genres=[9], switches=0 → 1
Constraints
  • 1 ≤ len(genres) ≤ 500
  • 1 ≤ genres[i] ≤ 10⁹
  • 0 ≤ switches ≤ min(len(genres), 25)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumPoints Without Neighbors MediumWords Hidden in a Text MediumTrees Built From Products MediumLongest Sum Chain Medium
loading Python…
Input
genres=[4, 4, 7, 4, 7, 7], switches=1
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.