Number of Music Playlists - Problem

Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:

  • Every song is played at least once
  • A song can only be played again only if k other songs have been played

Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 10^9 + 7.

Input & Output

Example 1 — Basic Case
$ Input: n = 3, goal = 3, k = 1
Output: 6
💡 Note: We have 3 songs and want a playlist of length 3. Each song must be played at least once, and we can't repeat a song until 1 other song has been played. Possible playlists: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
Example 2 — Higher K Value
$ Input: n = 2, goal = 3, k = 0
Output: 6
💡 Note: With k=0, we can repeat any song immediately. We have 2 songs, need length 3, both songs used at least once. Playlists: [1,1,2], [1,2,1], [1,2,2], [2,1,1], [2,1,2], [2,2,1].
Example 3 — Impossible Case
$ Input: n = 2, goal = 3, k = 3
Output: 0
💡 Note: With k=3, we need to play 3 other songs before repeating. But we only have 2 songs total, so it's impossible to repeat any song in a playlist of length 3.

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ goal ≤ 100
  • 0 ≤ k < n

Visualization

Tap to expand
Number of Music Playlists: n=3, goal=3, k=1Available Songs:[1, 2, 3]Playlist Length:3Cooldown (k):1Valid Playlists (All Songs Used, Respect Cooldown):[1, 2, 3][1, 3, 2][2, 1, 3][2, 3, 1][3, 1, 2][3, 2, 1]Answer:6Each playlist uses all 3 songs exactly once (permutations allowed since k=1)
Understanding the Visualization
1
Input
n=3 songs, goal=3 length, k=1 cooldown
2
Constraints
Every song used once, can't repeat until k others played
3
Output
6 possible valid playlists
Key Takeaway
🎯 Key Insight: Use DP to count playlists by tracking length and unique songs used
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
856 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