<?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>perimeter Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/perimeter/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/perimeter/</link>
	<description></description>
	<lastBuildDate>Fri, 17 Aug 2018 16:28: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>perimeter Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/perimeter/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 463. Island Perimeter</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-463-island-perimeter/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-463-island-perimeter/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 17 Aug 2018 16:25:05 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[O(mn)]]></category>
		<category><![CDATA[perimeter]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3576</guid>

					<description><![CDATA[<p>Problem You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-463-island-perimeter/">花花酱 LeetCode 463. Island Perimeter</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><iframe width="500" height="375" src="https://www.youtube.com/embed/ddFvTWmVUEA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn&#8217;t have &#8220;lakes&#8221; (water inside that isn&#8217;t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don&#8217;t exceed 100. Determine the perimeter of the island.</p>
<p><b>Example:</b></p>
<pre class="crayon:false">[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
<img src="https://leetcode.com/static/images/problemset/island.png" />
</pre>
<h1><strong>Solution: Counting</strong></h1>
<p>If a land has 0 neighbour, it contributes 4 to the perimeter</p>
<p>If a land has 1 neighbour, it contributes 3 to the perimeter</p>
<p>If a land has 2 neighbours, it contributes 2 to the perimeter</p>
<p>If a land has 3 neighbours, it contributes 1 to the perimeter</p>
<p>If a land has 4 neighbours, it contributes 0 to the perimeter</p>
<p>perimeter = area * 4 &#8211; total_neighbours</p>
<p>Time complexity: O(mn)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 148 ms
class Solution {
public:
  int islandPerimeter(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    if (grid.empty()) return 0;
    int m = grid.size();
    int n = grid[0].size();
    int area = 0;
    int conn = 0;

    for (int y = 0; y &lt; m; ++y)
      for (int x = 0; x &lt; n; ++x)
        if (grid[y][x] == 1) {
          ++area;
          if (y &gt; 0 &amp;&amp; grid[y - 1][x] == 1) ++conn;
          if (x &gt; 0 &amp;&amp; grid[y][x - 1] == 1) ++conn;
        }

    return area*4 - conn*2;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-463-island-perimeter/">花花酱 LeetCode 463. Island Perimeter</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/math/leetcode-463-island-perimeter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
