<?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>pre compute Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pre-compute/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pre-compute/</link>
	<description></description>
	<lastBuildDate>Sun, 13 Sep 2020 05:23:51 +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>pre compute Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pre-compute/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1582. Special Positions in a Binary Matrix</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1582-special-positions-in-a-binary-matrix/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1582-special-positions-in-a-binary-matrix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Sep 2020 05:22:59 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[pre compute]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7360</guid>

					<description><![CDATA[<p>Given a&#160;rows x cols&#160;matrix&#160;mat,&#160;where&#160;mat[i][j]&#160;is either&#160;0&#160;or&#160;1,&#160;return&#160;the number of special positions in&#160;mat. A position&#160;(i,j)&#160;is called&#160;special&#160;if&#160;mat[i][j] == 1&#160;and all other elements in row&#160;i&#160;and column&#160;j&#160;are&#160;0&#160;(rows and columns are&#160;0-indexed). Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1582-special-positions-in-a-binary-matrix/">花花酱 LeetCode 1582. Special Positions in a Binary Matrix</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>rows x cols</code>&nbsp;matrix&nbsp;<code>mat</code>,&nbsp;where&nbsp;<code>mat[i][j]</code>&nbsp;is either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>,&nbsp;return&nbsp;<em>the number of special positions in&nbsp;<code>mat</code>.</em></p>



<p>A position&nbsp;<code>(i,j)</code>&nbsp;is called&nbsp;<strong>special</strong>&nbsp;if&nbsp;<code>mat[i][j] == 1</code>&nbsp;and all other elements in row&nbsp;<code>i</code>&nbsp;and column&nbsp;<code>j</code>&nbsp;are&nbsp;<code>0</code>&nbsp;(rows and columns are&nbsp;<strong>0-indexed</strong>).</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[1,0,0],
&nbsp;             [0,0,<strong>1</strong>],
&nbsp;             [1,0,0]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> (1,2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[<strong>1</strong>,0,0],
&nbsp;             [0,<strong>1</strong>,0],
&nbsp;             [0,0,<strong>1</strong>]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> (0,0), (1,1) and (2,2) are special positions. 
</pre>



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



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



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



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



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



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



<h2><strong>Solution: Sum for each row and column</strong></h2>



<p>Brute force:<br>Time complexity: O(R*C*(R+C))<br>Space complexity: O(1)</p>



<p>We can pre-compute the sums for each row and each column, ans = sum(mat[r][c] == 1 and rsum[r] == 1 and csum[c] == 1)</p>



<p>Time complexity: O(R*C)<br>Space complexity: O(R+C)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int numSpecial(vector&lt;vector&lt;int&gt;&gt;&amp; mat) {
    const int rows = mat.size();
    const int cols = mat[0].size();
    vector&lt;int&gt; rs(rows);
    vector&lt;int&gt; cs(cols);
    for (int r = 0; r &lt; rows; ++r)
      for (int c = 0; c &lt; cols; ++c) {
        rs[r] += mat[r][c];
        cs[c] += mat[r][c];
      }
    int ans = 0;
    for (int r = 0; r &lt; rows; ++r)
      for (int c = 0; c &lt; cols; ++c)
        ans += mat[r][c] &amp;&amp; rs[r] == 1 &amp;&amp; cs[c] == 1;      
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1582-special-positions-in-a-binary-matrix/">花花酱 LeetCode 1582. Special Positions in a Binary Matrix</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/algorithms/array/leetcode-1582-special-positions-in-a-binary-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
