Your music player contains N
different songs and she wants to listen to L
(not necessarily different) songs during your trip. You create a playlist so that:html
K
other songs have been playedReturn the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7
.git
Example 1:github
Input: N = 3, L = 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], [3, 2, 1].
Example 2:數組
Input: N = 2, L = 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], [1, 2, 2]
Example 3:code
Input: N = 2, L = 3, K = 1 Output: 2 Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:htm
0 <= K < N <= L <= 100
這道題說是一個音樂播放器有N首歌,有個她想聽L首歌(能夠有重複),但須要知足兩個條件,一個是每首歌都必須至少播放1次,第二個是兩首重複歌的中間至少要有K首其餘的歌。提示告終果可能很是巨大,須要對一個超大數取餘。對於這類結果超大的數,基本不用懷疑,基本都是用動態規劃 Dynamic Programming 來作,這裏主要參考了 大神 optimisea 的帖子。首先就是要肯定 dp 的定義式,顯然這裏一維的 dp 數組是罩不住的,由於貌似有三個參數,N,L 和 K。可是否意味着須要個三維數組呢,其實也不用,並不關心全部的K值,可是對於N和L是必需要關注的,這裏用一個二維 dp 數組,其中 dp[i][j] 表示總共放了i首歌,其中j首是不一樣的。下面來考慮狀態轉移方程,在加入一首歌的時候,此時有兩種狀況:blog
綜上所述能夠獲得狀態轉移方程:ip
dp[i-1][j-1]*(N-(j-1)) + dp[i-1][j]*(j-k) (j > K) / dp[i][j] = \ dp[i-1][j-1]*(N-(j-1)) (j <= K)
參見代碼以下:leetcode
class Solution { public: int numMusicPlaylists(int N, int L, int K) { int M = 1e9 + 7; vector<vector<long>> dp(L + 1, vector<long>(N + 1)); dp[0][0] = 1; for (int i = 1; i <= L; ++i) { for (int j = 1; j <= N; ++j) { dp[i][j] = (dp[i - 1][j - 1] * (N - (j - 1))) % M; if (j > K) { dp[i][j] = (dp[i][j] + dp[i - 1][j] * (j - K) % M) % M; } } } return dp[L][N]; } };
Github 同步地址:get
https://github.com/grandyang/leetcode/issues/920
參考資料:
https://leetcode.com/problems/number-of-music-playlists/
https://leetcode.com/problems/number-of-music-playlists/discuss/178415/C%2B%2BJavaPython-DP-Solution