LeetCode 1905. Count Sub Islands

原题链接在这里:https://leetcode.com/problems/count-sub-islands/

题目:

You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

Return the number of islands in grid2 that are considered sub-islands.

 

Example 1:

Input: 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]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.

Example 2:

Input: 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]]
Output: 2 
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

 

Constraints:

  • m == grid1.length == grid2.length
  • n == grid1[i].length == grid2[i].length
  • 1 <= m, n <= 500
  • grid1[i][j] and grid2[i][j] are either 0 or 1.

题解:

In grid2, if we encounter 1, start dfs from there.

dfs is to check if corresponding grid1 cell has 1.

If out of index or grid2 is not longer 1, then dfs returns true, no violation.

Continues dfs and finally check if grid1 cell is 1.

If we check grid1 first, then it will stop dfs and didn't convert all 1s to 0s.

The same theory applies when check d1, d2, d3 and d4 and then return d1 && d2 && d3 && d4.

If we implement like dfs(i + 1, j) && dfs(i - 1, j) && dfs(i, j + 1) && dfs(i, j -1), then dfs(i + 1, j) == false will stop the rest 3 directions' dfs, then not all 1s connected are converted.

Time Complexity: O(m*n). m = grid1.legnth. n = grid1[0].length.

Space: O(m*n). Stack space.

AC Java: 

 1 class Solution {
 2     public int countSubIslands(int[][] grid1, int[][] grid2) {
 3         if(grid1 == null || grid2 == null || grid1.length != grid2.length || grid1[0].length != grid2[0].length){
 4             return 0;
 5         }
 6         
 7         int m = grid1.length;
 8         int n = grid1[0].length;
 9         int res = 0;
10         for(int i = 0; i < m; i++){
11             for(int j = 0; j < n; j++){
12                 if(grid2[i][j] == 1){
13                     if(dfs(grid1, grid2, i, j, m, n)){
14                         res++;
15                     }
16                 }
17             }
18         }
19         
20         return res;
21     }
22     
23     private boolean dfs(int [][] grid1, int [][] grid2, int i, int j, int m, int n){
24         if(i < 0 || i >= m || j < 0 || j >= n || grid2[i][j] != 1){
25             return true;
26         }
27         
28         grid2[i][j] = 0;
29         
30         boolean d1 = dfs(grid1, grid2, i + 1, j, m, n);
31         boolean d2 = dfs(grid1, grid2, i - 1, j, m, n);
32         boolean d3 = dfs(grid1, grid2, i, j + 1, m, n);
33         boolean d4 = dfs(grid1, grid2, i, j - 1, m, n);
34         return d1 && d2 && d3 && d4 && grid1[i][j] == 1;
35     }
36 }

类似Number of Islands.

posted @ 2022-05-16 06:56  Dylan_Java_NYC  阅读(31)  评论(0编辑  收藏  举报