<?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>O(n*m) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/onm/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/onm/</link>
	<description></description>
	<lastBuildDate>Wed, 13 Nov 2019 10:07:19 +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>O(n*m) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/onm/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1254. Number of Closed Islands</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Nov 2019 06:38:30 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[connected components]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[island]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(n*m)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5817</guid>

					<description><![CDATA[<p>Given a 2D&#160;grid&#160;consists of&#160;0s&#160;(land)&#160;and&#160;1s&#160;(water).&#160; An&#160;island&#160;is a maximal 4-directionally connected group of&#160;0s&#160;and a&#160;closed island&#160;is an island&#160;totally&#160;(all left, top, right, bottom) surrounded by&#160;1s. Return the number of&#160;closed&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/">花花酱 LeetCode 1254. Number of Closed 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[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1252 1253 1254 1255 Weekly Contest 162  - 刷题找工作" width="500" height="375" src="https://www.youtube.com/embed/1XMpzhFUvco?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a 2D&nbsp;<code>grid</code>&nbsp;consists of&nbsp;<code>0s</code>&nbsp;(land)&nbsp;and&nbsp;<code>1s</code>&nbsp;(water).&nbsp; An&nbsp;<em>island</em>&nbsp;is a maximal 4-directionally connected group of&nbsp;<code>0s</code>&nbsp;and a&nbsp;<em>closed island</em>&nbsp;is an island&nbsp;<strong>totally</strong>&nbsp;(all left, top, right, bottom) surrounded by&nbsp;<code>1s.</code></p>



<p>Return the number of&nbsp;<em>closed islands</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
Islands in gray are closed because they are completely surrounded by water (group of 1s).</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" alt=""/></figure>



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



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



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



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



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



<h2><strong>Solution: DFS/Backtracking</strong></h2>



<p>For each connected component, if it can reach the boundary then it&#8217;s not a closed island.</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 closedIsland(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    
    const int n = grid.size();
    const int m = grid[0].size();    
    function&lt;int(int, int)&gt; dfs = [&amp;](int x, int y) {
      if (x &lt; 0 || y &lt; 0 || x &gt;= m || y &gt;= n) return 0;      
      if (grid[y][x]++) return 1;
      return dfs(x + 1, y) &amp; dfs(x - 1, y) &amp; dfs(x, y + 1) &amp; dfs(x, y - 1);
    };
    
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; m; ++j)
        if (!grid[i][j]) ans += dfs(j, i);      
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/">花花酱 LeetCode 1254. Number of Closed 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/searching/leetcode-1254-number-of-closed-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
