{"id":3576,"date":"2018-08-17T09:25:05","date_gmt":"2018-08-17T16:25:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3576"},"modified":"2018-08-17T09:28:52","modified_gmt":"2018-08-17T16:28:52","slug":"leetcode-463-island-perimeter","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-463-island-perimeter\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 463. Island Perimeter"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 463. Island Perimeter - \u5237\u9898\u627e\u5de5\u4f5c EP13\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ddFvTWmVUEA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1>Problem<\/h1>\n<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>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\">[[0,1,0,0],\r\n [1,1,1,0],\r\n [0,1,0,0],\r\n [1,1,0,0]]\r\n\r\nAnswer: 16\r\nExplanation: The perimeter is the 16 yellow stripes in the image below:\r\n<img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/island.png\" \/>\r\n<\/pre>\n<h1><strong>Solution: Counting<\/strong><\/h1>\n<p>If a land has 0 neighbour, it contributes 4 to the\u00a0perimeter<\/p>\n<p>If a land has 1 neighbour, it contributes 3 to the\u00a0perimeter<\/p>\n<p>If a land has 2 neighbours, it contributes 2 to the\u00a0perimeter<\/p>\n<p>If a land has 3 neighbours, it contributes 1 to the perimeter<\/p>\n<p>If a land has 4 neighbours, it contributes 0 to the perimeter<\/p>\n<p>perimeter = area * 4 &#8211; total_neighbours<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 148 ms\r\nclass Solution {\r\npublic:\r\n  int islandPerimeter(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n    if (grid.empty()) return 0;\r\n    int m = grid.size();\r\n    int n = grid[0].size();\r\n    int area = 0;\r\n    int conn = 0;\r\n\r\n    for (int y = 0; y &lt; m; ++y)\r\n      for (int x = 0; x &lt; n; ++x)\r\n        if (grid[y][x] == 1) {\r\n          ++area;\r\n          if (y &gt; 0 &amp;&amp; grid[y - 1][x] == 1) ++conn;\r\n          if (x &gt; 0 &amp;&amp; grid[y][x - 1] == 1) ++conn;\r\n        }\r\n\r\n    return area*4 - conn*2;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<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>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[278,31,366,380],"class_list":["post-3576","post","type-post","status-publish","format-standard","hentry","category-math","tag-grid","tag-math","tag-omn","tag-perimeter","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3576"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3576\/revisions"}],"predecessor-version":[{"id":3580,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3576\/revisions\/3580"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}