{"id":4171,"date":"2018-10-06T22:25:38","date_gmt":"2018-10-07T05:25:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4171"},"modified":"2018-10-06T23:43:37","modified_gmt":"2018-10-07T06:43:37","slug":"leetcode-920-number-of-music-playlists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-920-number-of-music-playlists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 920. Number of Music Playlists"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Your music player contains\u00a0<code>N<\/code>\u00a0different songs and she wants to listen to\u00a0<code>L<\/code><strong>\u00a0<\/strong>(not necessarily different) songs during your trip. \u00a0You\u00a0create\u00a0a playlist so\u00a0that:<\/p>\n<ul>\n<li>Every song is played at least once<\/li>\n<li>A song can only be played again only if\u00a0<code>K<\/code>\u00a0other songs have been played<\/li>\n<\/ul>\n<p>Return the number of possible playlists.\u00a0\u00a0<strong>As the answer can be very large, return it modulo\u00a0<code>10^9 + 7<\/code><\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-1-1\">3<\/span>, L = <span id=\"example-input-1-2\">3<\/span>, K = <span id=\"example-input-1-3\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">6\r\n<strong>Explanation<\/strong>: <\/span>There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-2-1\">2<\/span>, L = <span id=\"example-input-2-2\">3<\/span>, K = <span id=\"example-input-2-3\">0<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">6\r\n<\/span><span id=\"example-output-1\"><strong>Explanation<\/strong>: <\/span>There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-3-1\">2<\/span>, L = <span id=\"example-input-3-2\">3<\/span>, K = <span id=\"example-input-3-3\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">2\r\n<strong>Explanation<\/strong>: <\/span>There are 2 possible playlists. [1, 2, 1], [2, 1, 2]\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= K &lt; N &lt;= L &lt;= 100<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>dp[i][j] := # of playlists of length i using j different songs.<\/p>\n<p>dp[i][j] = dp[i &#8211; 1][j &#8211; 1] * (N &#8211; (j &#8211; 1))\u00a0 +\u00a0 \/\/ Adding a new song. j &#8211; 1 used, choose any one from (N &#8211; (j &#8211; 1)) unused.<br \/>\ndp[i -1][j] * max(j &#8211; K, 0)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ Reuse an existing song.<\/p>\n<p>Time complexity: O(LN)<\/p>\n<p>Space complexity: O(LN) -&gt; O(N)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/O(LN)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int numMusicPlaylists(int N, int L, int K) {\r\n    constexpr long kMod = 1e9 + 7;\r\n    vector&lt;vector&lt;long&gt;&gt; dp(L + 1, vector&lt;long&gt;(N + 1, 0));\r\n    dp[0][0] = 1;\r\n    for (int i = 1; i &lt;= L; ++i)\r\n      for (int j = 1; j &lt;= min(i, N); ++j)\r\n        dp[i][j] = (dp[i - 1][j - 1] * (N - (j - 1)) + \r\n                    dp[i - 1][j] * max(j - K, 0)) % kMod;    \r\n    return dp[L][N];\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/O(N)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 0 ms\r\nclass Solution {\r\npublic:\r\n  int numMusicPlaylists(int N, int L, int K) {\r\n    constexpr long kMod = 1e9 + 7;\r\n    vector&lt;long&gt; dp(N + 1, 0);    \r\n    for (int i = 1; i &lt;= L; ++i) {\r\n      dp[0] = i == 1;\r\n      for (int j = min(i, N); j &gt;= 1; --j)\r\n        dp[j] = (dp[j - 1] * (N - (j - 1)) + \r\n                 dp[j] * max(j - K, 0)) % kMod;    \r\n    }\r\n    return dp[N];\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Your music player contains\u00a0N\u00a0different songs and she wants to listen to\u00a0L\u00a0(not necessarily different) songs during your trip. \u00a0You\u00a0create\u00a0a playlist so\u00a0that: Every song is played&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[122,18,217],"class_list":["post-4171","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-combination","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4171"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4171\/revisions"}],"predecessor-version":[{"id":4176,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4171\/revisions\/4176"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}