g1901_2000.s1905_count_sub_islands.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1901_2000.s1905_count_sub_islands;
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
// #Graph_Theory_I_Day_3_Matrix_Related_Problems
// #2022_05_11_Time_24_ms_(85.54%)_Space_142.6_MB_(18.19%)
public class Solution {
private int ans = 0;
public int countSubIslands(int[][] grid1, int[][] grid2) {
int count = 0;
for (int i = 0; i < grid2.length; i++) {
for (int j = 0; j < grid2[0].length; j++) {
if (grid2[i][j] == 1) {
ans = 1;
dfs(grid1, grid2, i, j);
count += ans;
}
}
}
return count;
}
private void dfs(int[][] grid1, int[][] grid2, int i, int j) {
if (i < 0 || j < 0 || i >= grid1.length || j >= grid1[0].length || grid2[i][j] == 0) {
return;
}
if (grid1[i][j] == 0) {
ans = 0;
}
grid2[i][j] = 0;
dfs(grid1, grid2, i - 1, j);
dfs(grid1, grid2, i + 1, j);
dfs(grid1, grid2, i, j + 1);
dfs(grid1, grid2, i, j - 1);
}
}