{"id":7318,"date":"2020-08-30T00:57:00","date_gmt":"2020-08-30T07:57:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7318"},"modified":"2020-09-14T22:23:16","modified_gmt":"2020-09-15T05:23:16","slug":"leetcode-1568-minimum-number-of-days-to-disconnect-island","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1568-minimum-number-of-days-to-disconnect-island\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1568. Minimum Number of Days to Disconnect Island"},"content":{"rendered":"\n<p>Given a 2D&nbsp;<code>grid<\/code>&nbsp;consisting&nbsp;of&nbsp;<code>1<\/code>s (land)&nbsp;and&nbsp;<code>0<\/code>s (water).&nbsp; An&nbsp;<em>island<\/em>&nbsp;is a maximal 4-directionally (horizontal or vertical) connected group of&nbsp;<code>1<\/code>s.<\/p>\n\n\n\n<p>The grid is said to be&nbsp;<strong>connected<\/strong>&nbsp;if we have&nbsp;<strong>exactly one&nbsp;island<\/strong>, otherwise is said&nbsp;<strong>disconnected<\/strong>.<\/p>\n\n\n\n<p>In one day, we are allowed to change&nbsp;<strong>any&nbsp;<\/strong>single land cell&nbsp;<code>(1)<\/code>&nbsp;into a water cell&nbsp;<code>(0)<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of days<\/em>&nbsp;to disconnect the grid.<\/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\/2020\/08\/13\/1926_island.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> We need at least 2 days to get a disconnected grid.\nChange land grid[1][1] and grid[0][2] to water and get 2 disconnected island.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1]]\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>Grid of full water is also disconnected ([[1,1]] -&gt; [[0,0]]), 0 islands.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0,1,0]]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,0,1,1],\n&nbsp;              [1,1,1,1,1],\n&nbsp;              [1,1,0,1,1],\n&nbsp;              [1,1,0,1,1]]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,0,1,1],\n&nbsp;              [1,1,1,1,1],\n&nbsp;              [1,1,0,1,1],\n&nbsp;              [1,1,1,1,1]]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= grid.length, grid[i].length &lt;= 30<\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>We need at most two days to disconnect an island. <br>1. check if we have more than one islands. (0 days)<br>2. For each 1 cell, change it to 0 and check how many islands do we have. (1 days)<br>3. Otherwise, 2 days<\/p>\n\n\n\n<p>Time complexity: O(m^2*n^2)<br>Space complexity: O(m*n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minDays(vector<vector<int>>& grid) {\n    const int n = grid.size();\n    const int m = grid[0].size();    \n    const vector<int> dirs{0, 1, 0, -1, 0};\n    vector<int> seen(n * m);    \n    function<void(int, int)> dfs = [&](int x, int y) {\n      if (x < 0 || y < 0 || x >= m || y >= n) return;\n      if (grid[y][x] == 0) return;\n      if (seen[y * m + x]++) return;      \n      for (int i = 0; i < 4; ++i)        \n        dfs(x + dirs[i], y + dirs[i + 1]);\n    };\n    \n    function<bool()> disconnected = [&]() {\n      int count = 0;\n      fill(begin(seen), end(seen), 0);\n      for (int y = 0; y < n; ++y)\n        for (int x = 0; x < m; ++x)\n          if (grid[y][x] &#038;&#038; !seen[y * m + x]) {\n            dfs(x, y);\n            if (++count > 1) return true;\n          }\n      return false;\n    };\n    \n    if (disconnected()) return 0;\n    \n    for (int y = 0; y < n; ++y)\n      for (int x = 0; x < m; ++x) {\n        if (!grid[y][x]) continue;\n        grid[y][x] = 0;\n        if (disconnected()) return 1;\n        grid[y][x] = 1;\n      }\n    return 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a 2D&nbsp;grid&nbsp;consisting&nbsp;of&nbsp;1s (land)&nbsp;and&nbsp;0s (water).&nbsp; An&nbsp;island&nbsp;is a maximal 4-directionally (horizontal or vertical) connected group of&nbsp;1s. The grid is said to be&nbsp;connected&nbsp;if we have&nbsp;exactly one&nbsp;island, otherwise&#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":[33,77,177],"class_list":["post-7318","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7318","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=7318"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7318\/revisions"}],"predecessor-version":[{"id":7383,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7318\/revisions\/7383"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}