Skip to content

Commit a67438a

Browse files
authored
Create number-of-music-playlists.cpp
1 parent 746e71d commit a67438a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/number-of-music-playlists.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n * l)
2+
// Space: O(l)
3+
4+
// Another better solution (generating function), you could refer to
5+
// https://leetcode.com/problems/number-of-music-playlists/solution/
6+
7+
class Solution {
8+
public:
9+
int numMusicPlaylists(int N, int L, int K) {
10+
static const int M = 1000000007;
11+
vector<vector<uint64_t>> dp(2, vector<uint64_t>(1 + L, 0ull));
12+
dp[0][0] = dp[1][1] = 1;
13+
for (int n = 1; n <= N; ++n) {
14+
dp[n % 2][n] = (dp[(n - 1) % 2][n - 1] * n) % M;
15+
for (int l = n + 1; l <= L; ++l) {
16+
dp[n % 2][l] = ((dp[n % 2][l - 1] * max(n - K, 0)) % M +
17+
(dp[(n - 1) % 2][l - 1] * n) % M) % M;
18+
}
19+
}
20+
return dp[N % 2][L];
21+
}
22+
};

0 commit comments

Comments
 (0)