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:
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 109 + 7
.
Example 1:
Input: n = 3, goal = 3, k = 1 Output: 6 Explanation: There are 6 possible playlists: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].
Example 2:
Input: n = 2, goal = 3, k = 0 Output: 6 Explanation: There are 6 possible playlists: [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], and [1, 2, 2].
Example 3:
Input: n = 2, goal = 3, k = 1 Output: 2 Explanation: There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].
Constraints:
0 <= k < n <= goal <= 100
This problem asks to find the number of playlists that can be created given n
songs, goal
number of songs to play, and k
as the minimum number of other songs that must be played before a song can be repeated.
The most efficient approach is using dynamic programming. We'll explore two versions: one with a larger space complexity and a space-optimized version.
This solution uses a 2D DP array f[i][j]
, where f[i][j]
represents the number of playlists of length i
using exactly j
different songs.
State Transition:
To understand the state transition, consider building a playlist of length i
using j
different songs. There are two possibilities for the i
-th song:
New Song: The i
-th song is a song that hasn't been played yet. In this case, we have n - (j - 1)
choices for the i
-th song (since j-1
songs have already been played), and the number of ways to create the playlist of length i-1
using j-1
songs is f[i-1][j-1]
. Thus, the number of playlists in this case is f[i-1][j-1] * (n - (j - 1))
.
Repeated Song: The i
-th song is a song that has already been played. To avoid boredom, at least k
other songs must have been played before repeating a song. This means we must have j > k
. We have j
choices for the i
-th song (any of the already played songs). The number of ways to create the playlist of length i-1
using j
songs is f[i-1][j]
. Therefore, the number of playlists in this case is f[i-1][j] * (j - k)
.
Combining both cases, we get the recurrence relation:
f[i][j] = f[i-1][j-1] * (n - (j - 1)) + (j > k ? f[i-1][j] * (j - k) : 0)
Base Case:
f[0][0] = 1
(an empty playlist is one valid playlist).
Final Answer:
The final answer is f[goal][n]
(playlists of length goal
using all n
songs).
Time and Space Complexity:
Notice that in the recurrence relation, f[i][j]
only depends on f[i-1][j-1]
and f[i-1][j]
. Therefore, we can optimize the space complexity to O(n) by using only two 1D arrays (or even just one if you're clever). The logic remains the same; only the DP array structure changes.
Time and Space Complexity:
n + 1
.The provided code snippets in multiple languages implement both solutions. The space-optimized versions are generally preferred for larger input sizes due to their reduced memory footprint. Remember that modulo operation (% mod
) is used throughout the code to prevent integer overflow.