<?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>connected Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/connected/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/connected/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:36:02 +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>connected Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/connected/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 695. Max Area of Island</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Oct 2017 06:34:14 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[connected]]></category>
		<category><![CDATA[DFS]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=552</guid>

					<description><![CDATA[<p>Problem: Given a non-empty 2D array grid of 0&#8217;s and 1&#8217;s, an island is a group of 1&#8216;s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/">花花酱 LeetCode 695. Max Area of Island</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><strong>Problem:</strong></p>
<p>Given a non-empty 2D array <code>grid</code> of 0&#8217;s and 1&#8217;s, an <b>island</b> is a group of <code>1</code>&#8216;s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.</p>
<p>Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]</pre><p>Given the above grid, return <code>6</code>. Note the answer is not 11, because the island must be connected 4-directionally.</p>
<p><b>Example 2:</b></p><pre class="crayon-plain-tag">[[0,0,0,0,0,0,0,0]]</pre><p>Given the above grid, return <code>0</code>.</p>
<p><b>Note:</b> The length of each dimension in the given <code>grid</code> does not exceed 50.</p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>Use DFS to find the connected components</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 32 ms
class Solution {
public:
    int maxAreaOfIsland(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
        int h = grid.size();
        if (h == 0) return 0;
        int w = grid[0].size();
        
        int ans = 0;
        for (int i = 0; i &lt; h; ++i)
            for (int j = 0; j &lt; w; ++j)
                ans = max(ans, area(grid, j, i, w, h));
        return ans;
    }
private:
    int area(vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int w, int h) {
        if (x &lt; 0 || y &lt; 0 || x &gt;= w || y &gt;= h || grid[y][x] == 0) return 0;
        
        grid[y][x] = 0;
        
        return area(grid, x - 1, y, w, h) 
             + area(grid, x + 1, y, w, h)
             + area(grid, x, y - 1, w, h)
             + area(grid, x, y + 1, w, h)
             + 1;            
    }
};</pre><p>&nbsp;</p>
<p><strong>Related problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/">[解题报告] LeetCode 200. Number of Islands</a></li>
<li><a href="http://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/">[解题报告] LeetCode 547. Friend Circles</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/">花花酱 LeetCode 695. Max Area of Island</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-695-max-area-of-island/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
