forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1905-count-sub-islands.kt
37 lines (33 loc) · 1.07 KB
/
1905-count-sub-islands.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
fun countSubIslands(grid1: Array<IntArray>, grid2: Array<IntArray>): Int {
fun isValid(i: Int, j: Int) = i in (0 until grid2.size) && j in (0 until grid2[0].size) && grid2[i][j] == 1
val dir = arrayOf(
intArrayOf(1,0),
intArrayOf(-1,0),
intArrayOf(0,1),
intArrayOf(0,-1)
)
fun dfs(i: Int, j: Int): Boolean {
if(grid1[i][j] != 1)
return false
grid2[i][j] = 0
var found = true
for((iD,jD) in dir){
val iN = i + iD
val jN = j + jD
if(isValid(iN, jN))
found = found and dfs(iN, jN)
}
return found
}
var count = 0
for(i in 0 until grid1.size){
for(j in 0 until grid1[0].size){
if(grid1[i][j] == 1 && grid2[i][j] == 1)
if(dfs(i, j) == true)
count++
}
}
return count
}
}