{"id":552,"date":"2017-10-07T23:34:14","date_gmt":"2017-10-08T06:34:14","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=552"},"modified":"2018-04-19T08:36:02","modified_gmt":"2018-04-19T15:36:02","slug":"leetcode-695-max-area-of-island","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-695-max-area-of-island\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 695. Max Area of Island"},"content":{"rendered":"<p><strong>Problem:<\/strong><\/p>\n<p>Given a non-empty 2D array\u00a0<code>grid<\/code>\u00a0of 0&#8217;s and 1&#8217;s, an\u00a0<b>island<\/b>\u00a0is a group of\u00a0<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>\n<p>Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">[[0,0,1,0,0,0,0,1,0,0,0,0,0],\r\n [0,0,0,0,0,0,0,1,1,1,0,0,0],\r\n [0,1,1,0,1,0,0,0,0,0,0,0,0],\r\n [0,1,0,0,1,1,0,0,1,0,1,0,0],\r\n [0,1,0,0,1,1,0,0,1,1,1,0,0],\r\n [0,0,0,0,0,0,0,0,0,0,1,0,0],\r\n [0,0,0,0,0,0,0,1,1,1,0,0,0],\r\n [0,0,0,0,0,0,0,1,1,0,0,0,0]]\r\n<\/pre>\n<p>Given the above grid, return\u00a0<code>6<\/code>. Note the answer is not 11, because the island must be connected 4-directionally.<\/p>\n<p><b>Example 2:<\/b><\/p>\n<pre>[[0,0,0,0,0,0,0,0]]<\/pre>\n<p>Given the above grid, return\u00a0<code>0<\/code>.<\/p>\n<p><b>Note:<\/b>\u00a0The length of each dimension in the given\u00a0<code>grid<\/code>\u00a0does not exceed 50.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<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 \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Use DFS to find the connected components<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 32 ms\r\nclass Solution {\r\npublic:\r\n    int maxAreaOfIsland(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {\r\n        int h = grid.size();\r\n        if (h == 0) return 0;\r\n        int w = grid[0].size();\r\n        \r\n        int ans = 0;\r\n        for (int i = 0; i &lt; h; ++i)\r\n            for (int j = 0; j &lt; w; ++j)\r\n                ans = max(ans, area(grid, j, i, w, h));\r\n        return ans;\r\n    }\r\nprivate:\r\n    int area(vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int w, int h) {\r\n        if (x &lt; 0 || y &lt; 0 || x &gt;= w || y &gt;= h || grid[y][x] == 0) return 0;\r\n        \r\n        grid[y][x] = 0;\r\n        \r\n        return area(grid, x - 1, y, w, h) \r\n             + area(grid, x + 1, y, w, h)\r\n             + area(grid, x, y - 1, w, h)\r\n             + area(grid, x, y + 1, w, h)\r\n             + 1;            \r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-200-number-of-islands\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 200. Number of Islands<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-547-friend-circles\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 547. Friend Circles<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a non-empty 2D array\u00a0grid\u00a0of 0&#8217;s and 1&#8217;s, an\u00a0island\u00a0is a group of\u00a01&#8216;s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[125,33],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-graph","tag-connected","tag-dfs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/552","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=552"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions\/2630"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}