<?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>2D Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/2d/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/2d/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Jul 2019 05:54:35 +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>2D Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/2d/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1139. Largest 1-Bordered Square</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1139-largest-1-bordered-square/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1139-largest-1-bordered-square/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Jul 2019 05:01:54 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5364</guid>

					<description><![CDATA[<p>Given a 2D&#160;grid&#160;of&#160;0s and&#160;1s, return the number of elements in&#160;the largest&#160;square&#160;subgrid that has all&#160;1s on its&#160;border, or&#160;0&#160;if such a subgrid&#160;doesn&#8217;t exist in the&#160;grid. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1139-largest-1-bordered-square/">花花酱 LeetCode 1139. Largest 1-Bordered Square</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>Given a 2D&nbsp;<code>grid</code>&nbsp;of&nbsp;<code>0</code>s and&nbsp;<code>1</code>s, return the number of elements in&nbsp;the largest&nbsp;<strong>square</strong>&nbsp;subgrid that has all&nbsp;<code>1</code>s on its&nbsp;<strong>border</strong>, or&nbsp;<code>0</code>&nbsp;if such a subgrid&nbsp;doesn&#8217;t exist in the&nbsp;<code>grid</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
<strong>Output:</strong> 9
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,1,0,0]]
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= grid.length &lt;= 100</code></li><li><code>1 &lt;= grid[0].length &lt;= 100</code></li><li><code>grid[i][j]</code>&nbsp;is&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code></li></ul>



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



<p>Compute the sums of all rectangles that has left-top corner at (0, 0) in O(m*n) time. <br>For each square and check whether its borders are all ones in O(1) time.</p>



<p>Time complexity: O(m*n*min(m,n))<br>Space complexity: O(m*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 largest1BorderedSquare(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    int n = grid.size();
    int m = grid[0].size();
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(m + 1));
    for (int i = 1; i &lt;= n; ++i)
      for (int j = 1; j &lt;= m; ++j)
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + grid[i - 1][j - 1];
    
    for (int len = min(n, m); len &gt; 0; --len)
      for (int x1 = 1, x2 = x1 + len - 1; x2 &lt;= m; ++x1, ++x2)
        for (int y1 = 1, y2 = y1 + len - 1; y2 &lt;= n; ++y1, ++y2)
          if (getArea(x1, y1, x2, y1, dp) == len 
              &amp;&amp; getArea(x1, y1, x1, y2, dp) == len
              &amp;&amp; getArea(x1, y2, x2, y2, dp) == len
              &amp;&amp; getArea(x2, y1, x2, y2, dp) == len)            
            return len * len;        
    return 0;
  }
private:
  int getArea(int x1, int y1, int x2, int y2, const vector&lt;vector&lt;int&gt;&gt;&amp; dp) {
    return dp[y2][x2] - dp[y2][x1 - 1] - dp[y1 - 1][x2] + dp[y1 - 1][x1 - 1];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1139-largest-1-bordered-square/">花花酱 LeetCode 1139. Largest 1-Bordered Square</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-1139-largest-1-bordered-square/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 85. Maximal Rectangle</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-85-maximal-rectangle/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-85-maximal-rectangle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 26 Jan 2019 18:21:08 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[rectangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4686</guid>

					<description><![CDATA[<p>Given a 2D binary matrix filled with 0&#8217;s and 1&#8217;s, find the largest rectangle containing only 1&#8217;s and return its area. Example: Input: [ ["1","0","1","0","0"],&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-85-maximal-rectangle/">花花酱 LeetCode 85. Maximal Rectangle</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>Given a 2D binary matrix filled with 0&#8217;s and 1&#8217;s, find the largest rectangle containing only 1&#8217;s and return its area.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input:</strong>
[
  ["1","0","1","0","0"],
  ["1","0","<strong>1</strong>","<strong>1</strong>","<strong>1</strong>"],
  ["1","1","<strong>1</strong>","<strong>1</strong>","<strong>1</strong>"],
  ["1","0","0","1","0"]
]
<strong>Output:</strong> 6</pre>



<h2>Solution: DP</h2>



<p>Time complexity: O(m^2*n)<br>Space complexity: O(mn)</p>



<p>dp[i][j] := max length of all 1 sequence ends with col j, at the i-th row.<br>transition: <br>dp[i][j] = 0 if matrix[i][j] == &#8216;0&#8217;<br>             = dp[i][j-1] + 1 if  matrix[i][j] == &#8216;1&#8217; </p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int maximalRectangle(vector&lt;vector&lt;char&gt; &gt; &amp;matrix) {
    int r = matrix.size();
    if (r == 0) return 0;
    int c = matrix[0].size();
  
    // dp[i][j] := max len of all 1s ends with col j at row i.
    vector&lt;vector&lt;int&gt;&gt; dp(r, vector&lt;int&gt;(c));

    for (int i = 0; i &lt; r; ++i)
      for (int j = 0; j &lt; c; ++j)
        dp[i][j] = (matrix[i][j] == '1') ? (j == 0 ? 1 : dp[i][j - 1] + 1) : 0;

    int ans = 0;

    for (int i = 0; i &lt; r; ++i)
      for (int j = 0; j &lt; c; ++j) {
        int len = INT_MAX;
        for (int k = i; k &lt; r; k++) {
          len = min(len, dp[k][j]);
          if (len == 0) break;
          ans = max(len * (k - i + 1), ans);
        }
    }

    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-85-maximal-rectangle/">花花酱 LeetCode 85. Maximal Rectangle</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-85-maximal-rectangle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
