<?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>ones Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ones/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ones/</link>
	<description></description>
	<lastBuildDate>Mon, 02 Dec 2019 07:21:52 +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>ones Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ones/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1277. Count Square Submatrices with All Ones</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1277-count-square-submatrices-with-all-ones/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1277-count-square-submatrices-with-all-ones/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 02 Dec 2019 07:21:33 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[ones]]></category>
		<category><![CDATA[square]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5912</guid>

					<description><![CDATA[<p>Given a&#160;m * n&#160;matrix of ones and zeros, return how many&#160;square&#160;submatrices have all ones. Example 1: Input: matrix = [ &#160; [0,1,1,1], &#160; [1,1,1,1], &#160;&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1277-count-square-submatrices-with-all-ones/">花花酱 LeetCode 1277. Count Square Submatrices with All Ones</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&nbsp;<code>m * n</code>&nbsp;matrix of ones and zeros, return how many&nbsp;<strong>square</strong>&nbsp;submatrices have all ones.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix =
[
&nbsp; [0,1,1,1],
&nbsp; [1,1,1,1],
&nbsp; [0,1,1,1]
]
<strong>Output:</strong> 15
<strong>Explanation:</strong> 
There are <strong>10</strong> squares of side 1.
There are <strong>4</strong> squares of side 2.
There is  <strong>1</strong> square of side 3.
Total number of squares = 10 + 4 + 1 = <strong>15</strong>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = 
[
  [1,0,1],
  [1,1,0],
  [1,1,0]
]
<strong>Output:</strong> 7
<strong>Explanation:</strong> 
There are <strong>6</strong> squares of side 1.  
There is <strong>1</strong> square of side 2. 
Total number of squares = 6 + 1 = <strong>7</strong>.
</pre>



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



<ul><li><code>1 &lt;= arr.length&nbsp;&lt;= 300</code></li><li><code>1 &lt;= arr[0].length&nbsp;&lt;= 300</code></li><li><code>0 &lt;= arr[i][j] &lt;= 1</code></li></ul>



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



<p>dp[i][j] := edge of largest square with bottom right corner at (i, j)<br>dp[i][j] = min(dp[i &#8211; 1][j], dp[i &#8211; 1][j &#8211; 1], dp[i][j &#8211; 1]) if m[i][j] == 1 else 0<br>ans = sum(dp)</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countSquares(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int n = matrix.size();
    const int m = matrix[0].size();
    // dp[i][j] := edge of largest square with right bottom corner at (i, j)
    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(m));
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; m; ++j) {
        dp[i][j] = matrix[i][j];
        if (i &amp;&amp; j &amp;&amp; dp[i][j])
          dp[i][j] = min({dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]}) + 1;
        ans += dp[i][j];
      }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1277-count-square-submatrices-with-all-ones/">花花酱 LeetCode 1277. Count Square Submatrices with All Ones</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-1277-count-square-submatrices-with-all-ones/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
