|
| 1 | +/** |
| 2 | + * [1568] Minimum Number of Days to Disconnect Island |
| 3 | + * |
| 4 | + * You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's. |
| 5 | + * The grid is said to be connected if we have exactly one island, otherwise is said disconnected. |
| 6 | + * In one day, we are allowed to change any single land cell (1) into a water cell (0). |
| 7 | + * Return the minimum number of days to disconnect the grid. |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land1.jpg" style="width: 500px; height: 169px;" /> |
| 11 | + * Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]] |
| 12 | + * Output: 2 |
| 13 | + * Explanation: We need at least 2 days to get a disconnected grid. |
| 14 | + * Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island. |
| 15 | + * |
| 16 | + * Example 2: |
| 17 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land2.jpg" style="width: 404px; height: 85px;" /> |
| 18 | + * Input: grid = [[1,1]] |
| 19 | + * Output: 2 |
| 20 | + * Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands. |
| 21 | + * |
| 22 | + * |
| 23 | + * Constraints: |
| 24 | + * |
| 25 | + * m == grid.length |
| 26 | + * n == grid[i].length |
| 27 | + * 1 <= m, n <= 30 |
| 28 | + * grid[i][j] is either 0 or 1. |
| 29 | + * |
| 30 | + */ |
| 31 | +pub struct Solution {} |
| 32 | + |
| 33 | +// problem: https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/ |
| 34 | +// discuss: https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/discuss/?currentPage=1&orderBy=most_votes&query= |
| 35 | + |
| 36 | +// submission codes start here |
| 37 | + |
| 38 | +impl Solution { |
| 39 | + // Credit: https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/solutions/3171061/just-a-runnable-solution/ |
| 40 | + pub fn min_days(grid: Vec<Vec<i32>>) -> i32 { |
| 41 | + let mut grid = grid; |
| 42 | + let mut islands = Self::count_islands(&grid); |
| 43 | + let (n, m) = (grid.len(), grid[0].len()); |
| 44 | + if islands > 1 || islands == 0 { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + for i in 0..n { |
| 49 | + for j in 0..m { |
| 50 | + if grid[i][j] == 1 { |
| 51 | + grid[i][j] = 0; |
| 52 | + islands = Self::count_islands(&grid); |
| 53 | + grid[i][j] = 1; |
| 54 | + if islands > 1 || islands == 0 { |
| 55 | + return 1; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + 2 |
| 62 | + } |
| 63 | + |
| 64 | + fn dfs_helper(x: usize, y: usize, grid: &Vec<Vec<i32>>, vis: &mut Vec<Vec<i32>>) { |
| 65 | + let dx = vec![1, -1, 0, 0]; |
| 66 | + let dy = vec![0, 0, 1, -1]; |
| 67 | + let (n, m) = (grid.len(), grid[0].len()); |
| 68 | + vis[x][y] = 1; |
| 69 | + for a in 0..4 { |
| 70 | + let nx = x as i32 + dx[a]; |
| 71 | + let ny = y as i32 + dy[a]; |
| 72 | + if nx >= 0 |
| 73 | + && ny >= 0 |
| 74 | + && nx < n as i32 |
| 75 | + && ny < m as i32 |
| 76 | + && vis[nx as usize][ny as usize] == 0 |
| 77 | + && grid[nx as usize][ny as usize] == 1 |
| 78 | + { |
| 79 | + Self::dfs_helper(nx as usize, ny as usize, grid, vis); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fn count_islands(grid: &Vec<Vec<i32>>) -> i32 { |
| 85 | + let mut islands = 0; |
| 86 | + let (n, m) = (grid.len(), grid[0].len()); |
| 87 | + let mut vis = vec![vec![0; m]; n]; |
| 88 | + for i in 0..n { |
| 89 | + for j in 0..m { |
| 90 | + if vis[i][j] == 0 && grid[i][j] == 1 { |
| 91 | + Self::dfs_helper(i, j, grid, &mut vis); |
| 92 | + islands += 1; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + islands |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// submission codes end |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_1568_example_1() { |
| 108 | + let grid = vec![vec![0, 1, 1, 0], vec![0, 1, 1, 0], vec![0, 0, 0, 0]]; |
| 109 | + |
| 110 | + let result = 2; |
| 111 | + |
| 112 | + assert_eq!(Solution::min_days(grid), result); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_1568_example_2() { |
| 117 | + let grid = vec![vec![1, 1]]; |
| 118 | + |
| 119 | + let result = 2; |
| 120 | + |
| 121 | + assert_eq!(Solution::min_days(grid), result); |
| 122 | + } |
| 123 | +} |
0 commit comments