[LeetCode] Number of Music Playlists

920. Number of Music Playlists

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int numMusicPlaylists(int n, int l, int k) {
long long dp[101][101] = {1}, mod = 1e9 + 7;
for(long long i = 1; i <= l; i++) {
for(long long j = 1; j <= n; j++) {
dp[i][j] = (dp[i-1][j-1] * (n - j + 1) + dp[i-1][j] * (max(0ll, j - k))) % mod;
}
}
return dp[l][n];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/04/PS/LeetCode/number-of-music-playlists/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.