[LeetCode] 920. Number of Music Playlists 音樂播放列表的個數



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:
  • Every song is played at least once
  • A song can only be played again only if K other songs have been played

Return the number of possible playlists.  As the answer can be very large, return it modulo 10^9 + 7.

Example 1:

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:

Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]

Note:

  1. 0 <= K < N <= L <= 100

這道題說是一個音樂播放器有N首歌,有個她想聽L首歌(可以有重復),但需要滿足兩個條件,一個是每首歌都必須至少播放1次,第二個是兩首重復歌的中間至少要有K首其他的歌。提示了結果可能非常巨大,需要對一個超大數取余。對於這類結果超大的數,基本不用懷疑,基本都是用動態規划 Dynamic Programming 來做,這里主要參考了 [大神 optimisea 的帖子](https://leetcode.com/problems/number-of-music-playlists/discuss/180338/DP-solution-that-is-Easy-to-understand)。首先就是要確定 dp 的定義式,顯然這里一維的 dp 數組是罩不住的,因為貌似有三個參數,N,L 和 K。但是否意味着需要個三維數組呢,其實也不用,並不關心所有的K值,但是對於N和L是必須要關注的,這里用一個二維 dp 數組,其中 dp[i][j] 表示總共放了i首歌,其中j首是不同的。下面來考慮狀態轉移方程,在加入一首歌的時候,此時有兩種情況:
  • 當加入的是一首新歌,則表示之前的 i-1 首歌中有 j-1 首不同的歌曲,其所有的組合情況都可以加上這首新歌,那么當前其實有 N-(j-1) 首新歌可以選。
  • 當加入的是一首重復的歌,則表示之前的 i-1 首歌中已經有了 j 首不同的歌,那么若沒有K的限制,則當前有 j 首重復的歌可以選。但是現在有了K的限制,意思是兩首重復歌中間必須要有K首其他的歌,則當前只有 j-K 首可以選。而當 j<K 時,其實這種情況是為0的。

綜上所述可以得到狀態轉移方程:


            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)

參見代碼如下:
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 同步地址:

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

https://leetcode.com/problems/number-of-music-playlists/discuss/180338/DP-solution-that-is-Easy-to-understand


[LeetCode All in One 題目講解匯總(持續更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM