-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCountSubIslands1905.kt
91 lines (75 loc) · 2.61 KB
/
CountSubIslands1905.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package medium
import java.util.*
fun countSubIslands(grid1: Array<IntArray>, grid2: Array<IntArray>): Int {
val totalRows = grid2.size
val totalCols = grid2[0].size
val visited = Array(totalRows) { BooleanArray(totalCols) { false } }
var subIslandsCount = 0
// Iterate on each cell in `grid1`
for (x in 0 until totalRows) {
for (y in 0 until totalCols) {
// If cell at the position (x,y) in the `grid2` is not visible,
// is a land cell in `grid2', and the island
// starting from this cell is a sub-island in `grid1`, then we
// increment the count od sub-island
if (!visited[x][y] &&
grid2.isCellLand(x, y) &&
isSubIsland(x, y, grid1, grid2, visited)
) {
subIslandsCount+=1
}
}
}
return subIslandsCount
}
private fun Array<IntArray>.isCellLand(x: Int, y: Int) = this[x][y] == 1
private fun isSubIsland(
x: Int,
y: Int,
grid1: Array<IntArray>,
grid2: Array<IntArray>,
visited: Array<BooleanArray>
): Boolean {
val directions = arrayOf(
intArrayOf(1, 0),
intArrayOf(0, 1),
intArrayOf(-1, 0),
intArrayOf(0, -1)
)
var isSubIsland = true
val totalRows = grid2.size
val totalCols = grid2[0].size
val pendingCells: Queue<IntArray> = LinkedList()
// Push the starting cell in the queue and mark it as visited
pendingCells.offer(intArrayOf(x, y))
visited[x][y] = true
while (pendingCells.isNotEmpty()) {
val curr: IntArray = pendingCells.poll()
val currX: Int = curr[0]
val currY: Int = curr[1]
// If the current position cell is not island cell in grid1,
// then the current island can't be sub-island
if (!grid1.isCellLand(currX, currY)) {
isSubIsland = false
}
for (dir in directions) {
val nextX: Int = currX + dir[0]
val nextY: Int = currY + dir[1]
// If the next cell is inside 'grid2', is never visitrd and
// is a lanf cell, then we traverse to the next cell.
if (
nextX >= 0 &&
nextY >= 0 &&
nextX < totalRows &&
nextY < totalCols &&
!visited[nextX][nextY] &&
grid2.isCellLand(nextX, nextY)
) {
// Push the next cell in the queue and mark it is as visited.
pendingCells.offer(intArrayOf(nextX, nextY))
visited[nextX][nextY] = true
}
}
}
return isSubIsland
}