<?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>cc Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/cc/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/cc/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Aug 2021 23:18:03 +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>cc Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/cc/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1905. Count Sub Islands</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Aug 2021 22:42:04 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[cc]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[island]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8587</guid>

					<description><![CDATA[<p>You are given two&#160;m x n&#160;binary matrices&#160;grid1&#160;and&#160;grid2&#160;containing only&#160;0&#8216;s (representing water) and&#160;1&#8216;s (representing land). An&#160;island&#160;is a group of&#160;1&#8216;s connected&#160;4-directionally&#160;(horizontal or vertical). Any cells outside of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/">花花酱 LeetCode 1905. Count Sub Islands</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 are given two&nbsp;<code>m x n</code>&nbsp;binary matrices&nbsp;<code>grid1</code>&nbsp;and&nbsp;<code>grid2</code>&nbsp;containing only&nbsp;<code>0</code>&#8216;s (representing water) and&nbsp;<code>1</code>&#8216;s (representing land). An&nbsp;<strong>island</strong>&nbsp;is a group of&nbsp;<code>1</code>&#8216;s connected&nbsp;<strong>4-directionally</strong>&nbsp;(horizontal or vertical). Any cells outside of the grid are considered water cells.</p>



<p>An island in&nbsp;<code>grid2</code>&nbsp;is considered a&nbsp;<strong>sub-island&nbsp;</strong>if there is an island in&nbsp;<code>grid1</code>&nbsp;that contains&nbsp;<strong>all</strong>&nbsp;the cells that make up&nbsp;<strong>this</strong>&nbsp;island in&nbsp;<code>grid2</code>.</p>



<p>Return the&nbsp;<em><strong>number</strong>&nbsp;of islands in&nbsp;</em><code>grid2</code>&nbsp;<em>that are considered&nbsp;<strong>sub-islands</strong></em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/06/10/test1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
<strong>Output:</strong> 2 
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
</pre>



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



<ul><li><code>m == grid1.length == grid2.length</code></li><li><code>n == grid1[i].length == grid2[i].length</code></li><li><code>1 &lt;= m, n &lt;= 500</code></li><li><code>grid1[i][j]</code>&nbsp;and&nbsp;<code>grid2[i][j]</code>&nbsp;are either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2><strong>Solution: Coloring</strong></h2>



<p>Give each island in grid1 a different color. Whiling using the same method to find island and coloring it in grid2, we also check whether the same cell in grid1 always has the same color.</p>



<p>Time complexity: O(mn)<br>Space complexity: O(1) modify in place or O(mn)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countSubIslands(vector&lt;vector&lt;int&gt;&gt;&amp; grid1, vector&lt;vector&lt;int&gt;&gt;&amp; grid2) {
    const int m = grid1.size();
    const int n = grid1[0].size();
    int valid = 1;
    
    function&lt;void(int, int, int, int)&gt; color = [&amp;](int i, int j, int c, int p) {
      if (i &lt; 0 || i &gt;= m || j &lt; 0 || j &gt;= n) return;
      auto&amp; gc = p ? grid2 : grid1;
      auto&amp; go = p ? grid1 : grid2;
      if (gc[i][j] != 1) return;
      gc[i][j] = c;
      if (p &amp;&amp; go[i][j] != c) valid = 0;
      color(i + 1, j, c, p);
      color(i - 1, j, c, p);
      color(i, j + 1, c, p);
      color(i, j - 1, c, p);
    };
        
    for (int i = 0, c = 2; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (grid1[i][j] == 1) color(i, j, c++, 0);
    
    int ans = 0;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (grid2[i][j] == 1 &amp;&amp; grid1[i][j]) {
          valid = 1;
          color(i, j, grid1[i][j], 1);
          ans += valid;
        }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/">花花酱 LeetCode 1905. Count Sub Islands</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/graph/leetcode-1905-count-sub-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1632. Rank Transform of a Matrix</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1632-rank-transform-of-a-matrix/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1632-rank-transform-of-a-matrix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 29 Oct 2020 03:38:19 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[cc]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7570</guid>

					<description><![CDATA[<p>Given an&#160;m x n&#160;matrix, return&#160;a new matrix&#160;answer&#160;where&#160;answer[row][col]&#160;is the&#160;rank&#160;of&#160;matrix[row][col]. The&#160;rank&#160;is an&#160;integer&#160;that represents how large an element is compared to other elements. It is calculated using the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1632-rank-transform-of-a-matrix/">花花酱 LeetCode 1632. Rank Transform of a 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 an&nbsp;<code>m x n</code>&nbsp;<code>matrix</code>, return&nbsp;<em>a new matrix&nbsp;</em><code>answer</code><em>&nbsp;where&nbsp;</em><code>answer[row][col]</code><em>&nbsp;is the&nbsp;</em><em><strong>rank</strong>&nbsp;of&nbsp;</em><code>matrix[row][col]</code>.</p>



<p>The&nbsp;<strong>rank</strong>&nbsp;is an&nbsp;<strong>integer</strong>&nbsp;that represents how large an element is compared to other elements. It is calculated using the following rules:</p>



<ul><li>The rank is an integer starting from&nbsp;<code>1</code>.</li><li>If two elements&nbsp;<code>p</code>&nbsp;and&nbsp;<code>q</code>&nbsp;are in the&nbsp;<strong>same row or column</strong>, then:<ul><li>If&nbsp;<code>p &lt; q</code>&nbsp;then&nbsp;<code>rank(p) &lt; rank(q)</code></li><li>If&nbsp;<code>p == q</code>&nbsp;then&nbsp;<code>rank(p) == rank(q)</code></li><li>If&nbsp;<code>p &gt; q</code>&nbsp;then&nbsp;<code>rank(p) &gt; rank(q)</code></li></ul></li><li>The&nbsp;<strong>rank</strong>&nbsp;should be as&nbsp;<strong>small</strong>&nbsp;as possible.</li></ul>



<p>It is guaranteed that&nbsp;<code>answer</code>&nbsp;is unique under the given rules.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/18/rank1.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[1,2],[3,4]]
<strong>Output:</strong> [[1,2],[2,3]]
<strong>Explanation:</strong>
The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
The rank of matrix[0][1] is 2 because matrix[0][1] &gt; matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][0] is 2 because matrix[1][0] &gt; matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][1] is 3 because matrix[1][1] &gt; matrix[0][1], matrix[1][1] &gt; matrix[1][0], and both matrix[0][1] and matrix[1][0] are rank 2.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/18/rank2.jpg" alt=""/></figure>



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



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/18/rank3.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[20,-21,14],[-19,4,19],[22,-47,24],[-19,4,19]]
<strong>Output:</strong> [[4,2,3],[1,3,4],[5,1,6],[1,3,4]]
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/18/rank4.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[7,3,6],[1,4,5],[9,8,2]]
<strong>Output:</strong> [[5,1,4],[1,2,3],[6,3,1]]
</pre>



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



<ul><li><code>m == matrix.length</code></li><li><code>n == matrix[i].length</code></li><li><code>1 &lt;= m, n &lt;= 500</code></li><li><code>-10<sup>9</sup>&nbsp;&lt;= matrix[row][col] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Union Find</strong></h2>



<p>Group cells by their values, process groups (cells that have the same value) in ascending order (smaller number has smaller rank).</p>



<p>For cells that are in the same row and same cols union them using union find, they should have the same rank which equals to max(max_rank_x[cols], max_rank_y[rows]) + 1.</p>



<p>Time complexity: O(m*n*(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 DSU {
public:
  DSU(int n): p_(n, -1) {}
  int find(int x) {
    return p_[x] == -1 ? x : p_[x] = find(p_[x]);
  }  
  void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x != y) p_[x] = y;    
  }
private:
  vector&lt;int&gt; p_;
};

class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; matrixRankTransform(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int m = matrix.size();
    const int n = matrix[0].size();
    vector&lt;vector&lt;int&gt;&gt; ans(m, vector&lt;int&gt;(n));
    map&lt;int, vector&lt;pair&lt;int, int&gt;&gt;&gt; mp; // val -&gt; {positions}
    for (int y = 0; y &lt; m; ++y)
      for (int x = 0; x &lt; n; ++x)
        mp[matrix[y][x]].emplace_back(x, y);
    vector&lt;int&gt; rx(n), ry(m);
    for (const auto&amp; [val, ps]: mp) {
      DSU dsu(n + m);
      vector&lt;vector&lt;pair&lt;int, int&gt;&gt;&gt; cc(n + m); // val -&gt; {positions}
      for (const auto&amp; [x, y]: ps)
        dsu.merge(x, y + n);
      for (const auto&amp; [x, y]: ps)        
        cc[dsu.find(x)].emplace_back(x, y);      
      for (const auto&amp; ps: cc) {
        int rank = 1;
        for (const auto&amp; [x, y]: ps)
          rank = max(rank, max(rx[x], ry[y]) + 1);
        for (const auto&amp; [x, y]: ps)
          rx[x] = ry[y] = ans[y][x] = rank;   
      }      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1632-rank-transform-of-a-matrix/">花花酱 LeetCode 1632. Rank Transform of a 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/graph/leetcode-1632-rank-transform-of-a-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
