{"id":1871,"date":"2018-02-24T20:27:41","date_gmt":"2018-02-25T04:27:41","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1871"},"modified":"2018-09-04T08:31:26","modified_gmt":"2018-09-04T15:31:26","slug":"leetcode-790-domino-and-tromino-tiling","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-790-domino-and-tromino-tiling\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 790. Domino and Tromino Tiling"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 790. Domino and Tromino Tiling - \u5237\u9898\u627e\u5de5\u4f5c EP171\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/S-fUTfqrdq8?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u6709\u4e24\u79cd\u4e0d\u540c\u5f62\u72b6\u7684\u9aa8\u724c(1&#215;2\u957f\u6761\u5f62\uff0cL\u578b\uff09\u65e0\u9650\u591a\u5757\u3002\u7ed9\u4f60\u4e00\u4e2a2xN\u7684\u677f\u5b50\uff0c\u95ee\u4e00\u5171\u6709\u591a\u5c11\u4e0d\u540c\u7684\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u5168\u8986\u76d6\u3002<\/p>\n<p>We have two types of tiles: a 2&#215;1 domino shape, and an &#8220;L&#8221; tromino shape. These shapes may be rotated.<\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">XX  &lt;- domino\r\n\r\nXX  &lt;- \"L\" tromino\r\nX\r\n<\/pre>\n<p>Given N, how many ways are there to tile a 2 x N board?\u00a0<strong>Return your answer modulo 10^9 + 7<\/strong>.<\/p>\n<p>(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)<\/p>\n<pre class=\"\">Example:\r\nInput: 3\r\nOutput: 5\r\nExplanation: \r\nThe five different ways are listed below, different letters indicates different tiles:\r\nXYZ XXZ XYY XXY XYY\r\nXYZ YYZ XZZ XYY XXY<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1904\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1903\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/790-ep171-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Idea: DP<\/strong><\/h1>\n<p>dp[i][0]: ways to cover i cols, both rows of i-th col are covered<br \/>\ndp[i][1]:\u00a0 ways to cover i cols, only top row of i-th col is covered<br \/>\ndp[i][2]:\u00a0 ways to cover i cols, only bottom row of i-th col is covered<\/p>\n<h1><strong>Solution 1: DP<\/strong><\/h1>\n<p>Time complexity: O(N)<\/p>\n<p>Space complexity: O(N)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int numTilings(int N) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;vector&lt;long&gt;&gt; dp(N + 1, vector&lt;long&gt;(3, 0));    \r\n    dp[0][0] = dp[1][0] = 1;\r\n    for (int i = 2; i &lt;= N; ++i) {\r\n      dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + dp[i - 1][1] + dp[i - 1][2]) % kMod;\r\n      dp[i][1] = (dp[i - 2][0] + dp[i - 1][2]) % kMod;\r\n      dp[i][2] = (dp[i - 2][0] + dp[i - 1][1]) % kMod;\r\n    }\r\n    \r\n    return dp[N][0];\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ V2<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">Since dp[i][1] always equals to dp[i][2], we can simplify a bit.\r\n\r\n\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int numTilings(int N) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;vector&lt;long&gt;&gt; dp(N + 1, vector&lt;long&gt;(2, 0));    \r\n    dp[0][0] = dp[1][0] = 1;\r\n    for (int i = 2; i &lt;= N; ++i) {\r\n      dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + 2 * dp[i - 1][1]) % kMod;\r\n      dp[i][1] = (dp[i - 2][0] + dp[i - 1][1]) % kMod;      \r\n    }\r\n    \r\n    return dp[N][0];\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: DP<\/strong><\/h1>\n<p>Another way to think about this problem<\/p>\n<p>define: dp[i] ways to completely covert the i*2 board.<\/p>\n<pre class=\"\">dp[0] = 1 # {}\r\ndp[1] = 1 # {|}\r\ndp[2] = 2 # {||, =}\r\ndp[3] = 5 # {|||, |=, =|,\u00a0\u230a\u2309,\u00a0\u2308\u230b} = dp[2] \u2297 {|} + dp[1] \u2297 {=} + dp[0] \u2297 {\u230a\u2309,\u00a0\u2308\u230b}\r\ndp[4] = 11 # dp[3] \u2297 {|} + dp[2] \u2297 {=} + dp[1] \u2297 {\u230a\u2309,\u00a0\u2308\u230b} + dp[0] \u2297 {\u230a\u00af\u230b,\u2308_\u2309}\r\ndp[5] = 24 # dp[4] \u2297 {|} + dp[3] \u2297 {=} + 2*(dp[2] + dp[1] + dp[0])\r\n...\r\ndp[n] = dp[n-1] + dp[n-2] + 2*(dp[n-3] + ... + dp[0])\r\n      = dp[n-1] + dp[n-3] + [dp[n-2] + dp[n-3] + 2*(dp[n-4] + ... + dp[0])]\r\n      = dp[n-1] + dp[n-3] + dp[n-1]\r\n      = 2*dp[n-1] + dp[n-3]<\/pre>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n  int numTilings(int N) {\r\n    constexpr int kMod = 1000000007;\r\n    vector&lt;long&gt; dp(N + 1, 1);\r\n    dp[2] = 2;\r\n    for (int i = 3; i &lt;= N; ++i)\r\n      dp[i] = (dp[i - 3] + dp[i - 1] * 2) % kMod;\r\n    return dp[N];\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6709\u4e24\u79cd\u4e0d\u540c\u5f62\u72b6\u7684\u9aa8\u724c(1&#215;2\u957f\u6761\u5f62\uff0cL\u578b\uff09\u65e0\u9650\u591a\u5757\u3002\u7ed9\u4f60\u4e00\u4e2a2xN\u7684\u677f\u5b50\uff0c\u95ee\u4e00\u5171\u6709\u591a\u5c11\u4e0d\u540c\u7684\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u5168\u8986\u76d6\u3002 We have two types of tiles: a 2&#215;1 domino shape, and an &#8220;L&#8221; tromino shape. These shapes may be rotated. XX &lt;- domino XX&#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":[8,18,177,223],"class_list":["post-1871","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-medium","tag-tiling","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1871","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=1871"}],"version-history":[{"count":22,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1871\/revisions"}],"predecessor-version":[{"id":3865,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1871\/revisions\/3865"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}