[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:html

  • 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.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

  1. 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

  • 当加入的是一首新歌,则表示以前的 i-1 首歌中有 j-1 首不一样的歌曲,其全部的组合状况均可以加上这首新歌,那么当前其实有 N-(j-1) 首新歌能够选。
  • 当加入的是一首重复的歌,则表示以前的 i-1 首歌中已经有了 j 首不一样的歌,那么若没有K的限制,则当前有 j 首重复的歌能够选。可是如今有了K的限制,意思是两首重复歌中间必需要有K首其余的歌,则当前只有 j-K 首能够选。而当 j<K 时,其实这种状况是为0的。

综上所述能够获得状态转移方程: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

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



LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索