diff options
-rw-r--r-- | min_num_days_disconnect_island.cpp | 56 | ||||
-rw-r--r-- | min_num_days_disconnect_island.py | 62 |
2 files changed, 96 insertions, 22 deletions
diff --git a/min_num_days_disconnect_island.cpp b/min_num_days_disconnect_island.cpp index f1598c8..992600a 100644 --- a/min_num_days_disconnect_island.cpp +++ b/min_num_days_disconnect_island.cpp @@ -1,25 +1,31 @@ +// 1568. Minimum Number of Days to Disconnect Island #include "leetcode.h" +/* + * you are given an 'm * n' binary grid where 1 represents land and 0 represents + * water. an island is a maximal 4 directionally horizontal or vertical + * connected group of 1's. the grid is said to be connected if we have exactly + * one island otherwise it is disconnected. in one day, we are allowed to change + * any single land cell 1 into a water cell 0. return the minimum number of days + * to disconnect the grid. + */ + class Solution { public: int minDays(vector<vector<int>> &grid) { - int islands = islandCount(grid); - int n = grid.size(), m = grid[0].size(); - if (islands > 1 || islands == 0) + int islands = cnt_islands(grid), n = grid.size(), m = grid[0].size(); + if (islands > 1 || !islands) return 0; - else { - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { + else + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) if (grid[i][j]) { grid[i][j] = 0; - islands = islandCount(grid); + islands = cnt_islands(grid); grid[i][j] = 1; - if (islands > 1 || islands == 1) + if (islands > 1 || !islands) return 1; } - } - } - } return 2; } @@ -28,23 +34,29 @@ private: void dfs(int x, int y, vector<vector<int>> &grid, vector<vector<int>> &vis) { int n = grid.size(), m = grid[0].size(); vis[x][y] = 1; - for (int a = 0; a < 4; a++) { - int nx = x + dx[a], ny = y + dy[a]; - if (nx >= 0 && ny >= 0 && nx < n && ny < m && !vis[nx][ny] && + for (int i = 0; i < 4; i++) { + int nx = x + dx[i], ny = y + dy[i]; + if (nx >= 0 and ny >= 0 and nx < n and ny < m and !vis[nx][ny] and grid[nx][ny]) - dfs(nx, nx, grid, vis); + dfs(nx, ny, grid, vis); } } - int islandCount(vector<vector<int>> &grid) { - int count = 0, n = grid.size(), m = grid[0].size(); + int cnt_islands(vector<vector<int>> &grid) { + int res = 0, n = grid.size(), m = grid[0].size(); vector<vector<int>> vis(n, vector<int>(m, 0)); - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) if (!vis[i][j] && grid[i][j]) { dfs(i, j, grid, vis); + res++; } - } - } - return count; + return res; } }; + +int main() { + Solution obj; + vvd(int) g1 = {{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}}, g2 = {{1, 1}}; + printf("%d\n", obj.minDays(g1)); // expect: 2 + printf("%d\n", obj.minDays(g2)); // expect: 2 +} diff --git a/min_num_days_disconnect_island.py b/min_num_days_disconnect_island.py new file mode 100644 index 0000000..14dfd44 --- /dev/null +++ b/min_num_days_disconnect_island.py @@ -0,0 +1,62 @@ +# 1568. Minimum Number of Days to Disconnect Island + +""" +you are given an 'm * n' binary grid where 1 repansents land and 0 repansents +water. an island is a maximal 4 directionally horizontal or vertical +connected group of 1's. the grid is said to be connected if we have exactly +one island otherwise it is disconnected. in one day, we are allowed to change +any single land cell 1 into a water cell 0. return the minimum number of days +to disconnect the grid. +""" + + +class Solution(object): + def minDays(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + cnt = 0 + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j]: + cnt += 1 + root = (i, j) + if cnt <= 1: + return cnt + vis, low, time, ans = {root}, {}, {}, [] + + def articulation_points(curr, parent): + low[curr] = time[curr] = len(vis) + children = 0 + i, j = curr + for x, y in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]: + if (x, y) == parent: + continue + if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y]: + if (x, y) not in vis: + vis.add((x, y)) + articulation_points((x, y), curr) + low[curr] = min(low[curr], low[(x, y)]) + children += 1 + if low[(x, y)] >= time[(curr)] and parent != (-1, -1): + ans.append([i, j]) + else: + low[curr] = min(low[curr], time[(x, y)]) + + if parent == (-1, -1) and children > 1: + ans.append([x, y]) + + articulation_points(root, (-1, -1)) + if len(vis) != cnt: + return 0 + elif ans: + return 1 + else: + return 2 + + +if __name__ == "__main__": + obj = Solution() + print(obj.minDays(grid=[[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]])) + print(obj.minDays(grid=[[1, 1]])) |