{"id":5761,"date":"2019-10-17T23:53:07","date_gmt":"2019-10-18T06:53:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5761"},"modified":"2019-10-19T02:35:14","modified_gmt":"2019-10-19T09:35:14","slug":"leetcode-1223-dice-roll-simulation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1223-dice-roll-simulation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1223. Dice Roll Simulation"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1223 Dice Roll Simulation - \u5237\u9898\u627e\u5de5\u4f5c EP275\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/3JOZcD-BRLE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>A die simulator generates a random number from 1 to 6 for each roll.&nbsp;You introduced a constraint to the generator such that it cannot roll the number&nbsp;<code>i<\/code>&nbsp;more than&nbsp;<code>rollMax[i]<\/code>&nbsp;(1-indexed)&nbsp;<strong>consecutive<\/strong>&nbsp;times.&nbsp;<\/p>\n\n\n\n<p>Given an array of integers&nbsp;<code>rollMax<\/code>&nbsp;and an integer&nbsp;<code>n<\/code>, return the number of distinct sequences that can be obtained with exact&nbsp;<code>n<\/code>&nbsp;rolls.<\/p>\n\n\n\n<p>Two sequences are considered different if at least one element differs from each other. Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;<code>10^9 + 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, rollMax = [1,1,2,2,2,3]\n<strong>Output:<\/strong> 34\n<strong>Explanation:<\/strong> There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, rollMax = [1,1,1,1,1,1]\n<strong>Output:<\/strong> 30\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, rollMax = [1,1,1,2,2,3]\n<strong>Output:<\/strong> 181\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 5000<\/code><\/li><li><code>rollMax.length == 6<\/code><\/li><li><code>1 &lt;= rollMax[i] &lt;= 15<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solutions: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides1.png\" alt=\"\" class=\"wp-image-5767\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Naive one:<\/p>\n\n\n\n<p>def: dp[i][j][k] := # of sequences ends with k consecutive j after i rolls<br>Init: dp[1][*][1] = 1<br><br>transition:<br>dp[i][j][1] = sum(dp[i-1][p][*]), p != j<br>dp[i][j][k + 1] = dp[i-1][j]][k]<\/p>\n\n\n\n<p>Time complexity: O(n*6*6*15)<br>Space complexity: O(n*6*15) -&gt; O(6*15)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int dieSimulator(int n, vector<int>& rollMax) {\n    constexpr int kMaxRolls = 15;\n    constexpr int kMod = 1e9 + 7;\n    \n    \/\/ dp[i][j][k] := # of sequences ends with k consecutive j after i rolls\n    vector<vector<vector<int>>> dp(n + 1, \n        vector<vector<int>>(6, vector<int>(kMaxRolls + 1))); \n    \n    for (int j = 0; j < 6; ++j)\n      dp[1][j][1] = 1; \/\/ 1 step, 1 dice, 1 way\n    \n    for (int i = 2; i <= n; ++i)\n      for (int j = 0; j < 6; ++j)\n        for (int p = 0; p < 6; ++p)\n          for (int k = 1; k <= rollMax[p]; ++k)\n            if (p != j) \/\/ not the same dice\n              dp[i][j][1] = (dp[i][j][1] + dp[i - 1][p][k]) % kMod;\n            else if (k < rollMax[p]) \/\/ same dice, make sure k + 1 <= rollMax\n              dp[i][j][k + 1] = (dp[i][j][k + 1] + dp[i - 1][p][k]) % kMod;\n    \n    int ans = 0;\n    for (int j = 0; j < 6; ++j)\n      for (int k = 1; k <= rollMax[j]; ++k)\n        ans = (ans + dp[n][j][k]) % kMod;\n  \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: DP with compressed state<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides2.png\" alt=\"\" class=\"wp-image-5768\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1223-ep275-slides2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i][j] := # of sequences of length i end with j<br>dp[i][j] := sum(dp[i-1]) - invalid<\/p>\n\n\n\n<p>Time complexity: O(n*6)<br>Space complexity: O(n*6)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int dieSimulator(int n, vector<int>& rollMax) {\n    constexpr int kMod = 1e9 + 7;    \n    \/\/ dp[i][j] := # of sequences ends with j after i rolls    \n    vector<vector<int>> dp(n + 1, vector<int>(7));    \n    vector<int> sums(n + 1); \/\/ sums[i] := sum(dp[i])\n    \n    for (int j = 0; j < 6; ++j)\n      sums[1] += dp[1][j] = 1; \/\/ 1 roll, 1 dice, 1 way\n    \n    for (int i = 2; i <= n; ++i)\n      for (int j = 0; j < 6; ++j) {        \n        const int k = i - rollMax[j];\n        const int invalid = k <= 1 ? max(k, 0) : sums[k - 1] - dp[k - 1][j];\n        dp[i][j] = ((sums[i - 1] - invalid) % kMod + kMod) % kMod;\n        sums[i] = (sums[i] + dp[i][j]) % kMod;\n      }\n  \n    return sums[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A die simulator generates a random number from 1 to 6 for each roll.&nbsp;You introduced a constraint to the generator such that it cannot roll&#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":[18,177],"class_list":["post-5761","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5761","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=5761"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5761\/revisions"}],"predecessor-version":[{"id":5769,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5761\/revisions\/5769"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}