{"id":6603,"date":"2020-04-12T00:08:44","date_gmt":"2020-04-12T07:08:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6603"},"modified":"2020-04-14T12:58:41","modified_gmt":"2020-04-14T19:58:41","slug":"leetcode-1411-number-of-ways-to-paint-n-x-3-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1411-number-of-ways-to-paint-n-x-3-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1411. Number of Ways to Paint N \u00d7 3 Grid"},"content":{"rendered":"\n<p>You have a&nbsp;<code>grid<\/code>&nbsp;of size&nbsp;<code>n x 3<\/code>&nbsp;and you want to paint each cell of the grid with exactly&nbsp;one of the three colours:&nbsp;<strong>Red<\/strong>,&nbsp;<strong>Yellow<\/strong>&nbsp;or&nbsp;<strong>Green<\/strong>&nbsp;while making sure that no two adjacent cells have&nbsp;the same colour (i.e no two cells that share vertical or horizontal sides have the same colour).<\/p>\n\n\n\n<p>You are given&nbsp;<code>n<\/code>&nbsp;the number of rows of the grid.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of ways<\/em>&nbsp;you can paint this&nbsp;<code>grid<\/code>. As the answer may grow large, the answer&nbsp;<strong>must be<\/strong>&nbsp;computed 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 = 1\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> There are 12 possible way to paint the grid as shown:\n\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\n<strong>Output:<\/strong> 54\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\n<strong>Output:<\/strong> 246\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7\n<strong>Output:<\/strong> 106494\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5000\n<strong>Output:<\/strong> 30228214\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == grid.length<\/code><\/li><li><code>grid[i].length == 3<\/code><\/li><li><code>1 &lt;= n &lt;= 5000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][0] := # of ways to paint i rows with 2 different colors at the i-th row<br>dp[i][1] := # of ways to paint i rows with 3 different colors at the i-th row<br>dp[1][0] = dp[1][1] = 6<br>dp[i][0] = dp[i-1][0] * 3 + dp[i-1][1] * 2<br>dp[i][1] = dp[i-1][0] * 2 + dp[i-1][1] * 2<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 numOfWays(int n) {\n    constexpr int kMod = 1e9 + 7;\n    \/\/ dp[i][0] = # of ways to paint i rows with i-th row has 2 colors (e.g. 121)\n    \/\/ dp[i][1] = # of ways to paint i rows with i-th row has 3 colors (e.g. 123)\n    \/\/ dp[1][0] = dp[1][1] = 6.\n    vector<vector<long>> dp(n + 1, vector<long>(2, 6));        \n    for (int i = 2; i <= n; ++i) {\n      \/\/ 121 => 2 colors x 3 {212, 232, 313}, 3 colors x 2 {213, 312}\n      \/\/ 123 => 2 colors x 2 {212, 232}, 3 colors x 2 {231, 312}      \n      dp[i][0] = (dp[i - 1][0] * 3 + dp[i - 1][1] * 2) % kMod;\n      dp[i][1] = (dp[i - 1][0] * 2 + dp[i - 1][1] * 2) % kMod;\n    }\n    return (dp[n][0] + dp[n][1]) % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def numOfWays(self, n: int) -&gt; int:\n    kMod = 10**9 + 7\n    dp2, dp3 = 6, 6\n    for i in range(1, n):\n      t2, t3 = dp2 * 3 + dp3 * 2, dp2 * 2 + dp3 * 2\n      dp2, dp3 = t2 % kMod, t3 % kMod\n    return (dp2 + dp3) % kMod\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: DP w\/ Matrix Chain Multiplication<\/strong><\/h2>\n\n\n\n<p>ans = {6, 6} * {{3, 2}, {2,2}} ^ (n-1)<\/p>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/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 numOfWays(int n) {\n    constexpr long kMod = 1e9 + 7;\n    vector<vector<long>> ans{{6, 6}}; \/\/ 1x2\n    vector<vector<long>> M{{3, 2},{2,2}}; \/\/ 2x2\n    auto mul = [kMod](const vector<vector<long>>& A, \n                      const vector<vector<long>>& B) {\n      const int m = A.size(); \/\/ m * n\n      const int n = B.size(); \/\/ n * p\n      const int p = B[0].size();\n      vector<vector<long>> C(m, vector<long>(p));\n      for (int i = 0; i < m; ++i)\n        for (int j = 0; j < p; ++j)\n          for (int k = 0; k < n; ++k)\n            C[i][j] += (A[i][k] * B[k][j]) % kMod;\n      return C;\n    };\n    --n;\n    while (n) {      \n      if (n &#038; 1) ans = mul(ans, M); \/\/ ans = ans * M;\n      M = mul(M, M); \/\/ M = M^2\n      n >>= 1;\n    }\n    \/\/ ans = ans0 * M^(n-1)\n    return (ans[0][0] + ans[0][1]) % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have a&nbsp;grid&nbsp;of size&nbsp;n x 3&nbsp;and you want to paint each cell of the grid with exactly&nbsp;one of the three colours:&nbsp;Red,&nbsp;Yellow&nbsp;or&nbsp;Green&nbsp;while making sure that no&#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,585],"class_list":["post-6603","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-combination","tag-dp","tag-hard","tag-matrix-chain","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6603","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=6603"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6603\/revisions"}],"predecessor-version":[{"id":6620,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6603\/revisions\/6620"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}