{"id":8587,"date":"2021-08-15T15:42:04","date_gmt":"2021-08-15T22:42:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8587"},"modified":"2021-08-15T16:18:03","modified_gmt":"2021-08-15T23:18:03","slug":"leetcode-1905-count-sub-islands","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1905-count-sub-islands\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1905. Count Sub Islands"},"content":{"rendered":"\n<p>You are given two&nbsp;<code>m x n<\/code>&nbsp;binary matrices&nbsp;<code>grid1<\/code>&nbsp;and&nbsp;<code>grid2<\/code>&nbsp;containing only&nbsp;<code>0<\/code>&#8216;s (representing water) and&nbsp;<code>1<\/code>&#8216;s (representing land). An&nbsp;<strong>island<\/strong>&nbsp;is a group of&nbsp;<code>1<\/code>&#8216;s connected&nbsp;<strong>4-directionally<\/strong>&nbsp;(horizontal or vertical). Any cells outside of the grid are considered water cells.<\/p>\n\n\n\n<p>An island in&nbsp;<code>grid2<\/code>&nbsp;is considered a&nbsp;<strong>sub-island&nbsp;<\/strong>if there is an island in&nbsp;<code>grid1<\/code>&nbsp;that contains&nbsp;<strong>all<\/strong>&nbsp;the cells that make up&nbsp;<strong>this<\/strong>&nbsp;island in&nbsp;<code>grid2<\/code>.<\/p>\n\n\n\n<p>Return the&nbsp;<em><strong>number<\/strong>&nbsp;of islands in&nbsp;<\/em><code>grid2<\/code>&nbsp;<em>that are considered&nbsp;<strong>sub-islands<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/06\/10\/test1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.\nThe 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/06\/03\/testcasex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]\n<strong>Output:<\/strong> 2 \n<strong>Explanation: <\/strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.\nThe 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid1.length == grid2.length<\/code><\/li><li><code>n == grid1[i].length == grid2[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 500<\/code><\/li><li><code>grid1[i][j]<\/code>&nbsp;and&nbsp;<code>grid2[i][j]<\/code>&nbsp;are either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Coloring<\/strong><\/h2>\n\n\n\n<p>Give each island in grid1 a different color. Whiling using the same method to find island and coloring it in grid2, we also check whether the same cell in grid1 always has the same color.<\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(1) modify in place or O(mn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {\n    const int m = grid1.size();\n    const int n = grid1[0].size();\n    int valid = 1;\n    \n    function<void(int, int, int, int)> color = [&](int i, int j, int c, int p) {\n      if (i < 0 || i >= m || j < 0 || j >= n) return;\n      auto& gc = p ? grid2 : grid1;\n      auto& go = p ? grid1 : grid2;\n      if (gc[i][j] != 1) return;\n      gc[i][j] = c;\n      if (p && go[i][j] != c) valid = 0;\n      color(i + 1, j, c, p);\n      color(i - 1, j, c, p);\n      color(i, j + 1, c, p);\n      color(i, j - 1, c, p);\n    };\n        \n    for (int i = 0, c = 2; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        if (grid1[i][j] == 1) color(i, j, c++, 0);\n    \n    int ans = 0;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        if (grid2[i][j] == 1 &#038;&#038; grid1[i][j]) {\n          valid = 1;\n          color(i, j, grid1[i][j], 1);\n          ans += valid;\n        }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;m x n&nbsp;binary matrices&nbsp;grid1&nbsp;and&nbsp;grid2&nbsp;containing only&nbsp;0&#8216;s (representing water) and&nbsp;1&#8216;s (representing land). An&nbsp;island&nbsp;is a group of&nbsp;1&#8216;s connected&nbsp;4-directionally&nbsp;(horizontal or vertical). Any cells outside of the&#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":[665,33,77,433,177],"class_list":["post-8587","post","type-post","status-publish","format-standard","hentry","category-graph","tag-cc","tag-dfs","tag-graph","tag-island","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8587","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=8587"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8587\/revisions"}],"predecessor-version":[{"id":8589,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8587\/revisions\/8589"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}