标签:Count grid1 grid2 int dfs length && Islands 1905
原题链接在这里: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]
andgrid2[i][j]
are either0
or1
.
题解:
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 }
标签:Count,grid1,grid2,int,dfs,length,&&,Islands,1905 来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16275568.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。