<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>matrix chain Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/matrix-chain/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/matrix-chain/</link>
	<description></description>
	<lastBuildDate>Tue, 14 Apr 2020 19:58:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>matrix chain Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/matrix-chain/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1411. Number of Ways to Paint N × 3 Grid</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1411-number-of-ways-to-paint-n-x-3-grid/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1411-number-of-ways-to-paint-n-x-3-grid/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Apr 2020 07:08:44 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[matrix chain]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6603</guid>

					<description><![CDATA[<p>You have a&#160;grid&#160;of size&#160;n x 3&#160;and you want to paint each cell of the grid with exactly&#160;one of the three colours:&#160;Red,&#160;Yellow&#160;or&#160;Green&#160;while making sure that no&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1411-number-of-ways-to-paint-n-x-3-grid/">花花酱 LeetCode 1411. Number of Ways to Paint N × 3 Grid</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>You are given&nbsp;<code>n</code>&nbsp;the number of rows of the grid.</p>



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



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1
<strong>Output:</strong> 12
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown:

</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2
<strong>Output:</strong> 54
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3
<strong>Output:</strong> 246
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 7
<strong>Output:</strong> 106494
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5000
<strong>Output:</strong> 30228214
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: DP</strong></h2>



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



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numOfWays(int n) {
    constexpr int kMod = 1e9 + 7;
    // dp[i][0] = # of ways to paint i rows with i-th row has 2 colors (e.g. 121)
    // dp[i][1] = # of ways to paint i rows with i-th row has 3 colors (e.g. 123)
    // dp[1][0] = dp[1][1] = 6.
    vector&lt;vector&lt;long&gt;&gt; dp(n + 1, vector&lt;long&gt;(2, 6));        
    for (int i = 2; i &lt;= n; ++i) {
      // 121 =&gt; 2 colors x 3 {212, 232, 313}, 3 colors x 2 {213, 312}
      // 123 =&gt; 2 colors x 2 {212, 232}, 3 colors x 2 {231, 312}      
      dp[i][0] = (dp[i - 1][0] * 3 + dp[i - 1][1] * 2) % kMod;
      dp[i][1] = (dp[i - 1][0] * 2 + dp[i - 1][1] * 2) % kMod;
    }
    return (dp[n][0] + dp[n][1]) % kMod;
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def numOfWays(self, n: int) -&amp;gt; int:
    kMod = 10**9 + 7
    dp2, dp3 = 6, 6
    for i in range(1, n):
      t2, t3 = dp2 * 3 + dp3 * 2, dp2 * 2 + dp3 * 2
      dp2, dp3 = t2 % kMod, t3 % kMod
    return (dp2 + dp3) % kMod</pre>
</div></div>



<h2><strong>Solution 2: DP w/ Matrix Chain Multiplication</strong></h2>



<p>ans = {6, 6} * {{3, 2}, {2,2}} ^ (n-1)</p>



<p>Time complexity: O(logn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numOfWays(int n) {
    constexpr long kMod = 1e9 + 7;
    vector&lt;vector&lt;long&gt;&gt; ans{{6, 6}}; // 1x2
    vector&lt;vector&lt;long&gt;&gt; M{{3, 2},{2,2}}; // 2x2
    auto mul = [kMod](const vector&lt;vector&lt;long&gt;&gt;&amp; A, 
                      const vector&lt;vector&lt;long&gt;&gt;&amp; B) {
      const int m = A.size(); // m * n
      const int n = B.size(); // n * p
      const int p = B[0].size();
      vector&lt;vector&lt;long&gt;&gt; C(m, vector&lt;long&gt;(p));
      for (int i = 0; i &lt; m; ++i)
        for (int j = 0; j &lt; p; ++j)
          for (int k = 0; k &lt; n; ++k)
            C[i][j] += (A[i][k] * B[k][j]) % kMod;
      return C;
    };
    --n;
    while (n) {      
      if (n &amp; 1) ans = mul(ans, M); // ans = ans * M;
      M = mul(M, M); // M = M^2
      n &gt;&gt;= 1;
    }
    // ans = ans0 * M^(n-1)
    return (ans[0][0] + ans[0][1]) % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1411-number-of-ways-to-paint-n-x-3-grid/">花花酱 LeetCode 1411. Number of Ways to Paint N × 3 Grid</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1411-number-of-ways-to-paint-n-x-3-grid/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
