Artist Blocks in a Playlist
Easy · Greedy & Intervals · about 15 min
A playlist is written as a string: each lowercase letter is the artist of one song, in play order. Cut the playlist into as many back-to-back blocks as possible so that every artist's songs all fall in one block. Return the lengths of the blocks from left to right.
playlisthas 0 to 10^5 lowercase letters. An empty playlist gives[].
artist_blocks("abacdcdeffe") → [3, 4, 4] (blocks "aba", "cdcd", "effe")
Hints
- First find where each artist plays for the last time.
- Walk the playlist and keep the farthest last-position of every artist in the current block.
- When your position equals that farthest point, close the block and record its length.
Learn the pattern first: the Greedy & Intervals lesson.
More Greedy & Intervals problems
- Stepping Stones Across the Creek (Easy)
- Squeeze In a Booking (Medium)
- Clear the Talk Schedule (Medium)
- Electric Bus Loop (Medium)
- Team Bonus Tokens (Hard)
- Road Trip Fuel Stops (Hard)