000920-Number-of-Music-Playlists
Problem
Solution
class Solution:
def numMusicPlaylists(self, n: int, goal: int, k: int) -> int:
MOD = 10**9 + 7
# dp[i][j] represents the number of possible playlists with i and j distinct songs
dp = [[0] * (goal+1) for _ in range(n+1)]
dp[0][0] = 1
for i in range(1, n+1):
for j in range(i, goal+1):
# 1. choose a new song, --> dp[i-1][j-1]*(n-i+1)
# 2. choose a song already been played --> dp[i][j-1]*max(i-k,0)
dp[i][j] = (dp[i-1][j-1]*(n-i+1) + dp[i][j-1]*max(i-k,0)) % MOD
return dp[n][goal] Previous000864-Shortest-Path-to-Get-All-KeysNext001218-Longest-Arithmetic-Subsequence-of-Given-Difference
Last updated