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 109 + 7
.
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].
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].
Input: n = 2, goal = 3, k = 1 Output: 2 Explanation: There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].
0 <= k < n <= goal <= 100
impl Solution {
pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
let n = n as usize;
let goal = goal as usize;
let k = k as usize;
let mut dp = vec![vec![0; n + 1]; goal + 1];
dp[0][0] = 1;
for i in 0..goal {
for j in 0..=n.min(i) {
if j < n {
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (n - j)) % 1_000_000_007;
}
if j > k {
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * (j - k)) % 1_000_000_007;
}
}
}
dp[goal][n] as i32
}
}