Skip to content

Commit 63a186d

Browse files
committed
Solve 0286 with kotlin
1 parent dfef0bc commit 63a186d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

kotlin/0286-walls-and-gates.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
fun wallsAndGates(rooms: Array<IntArray>): Unit {
3+
val queue: Queue<Pair<Int, Int>> = LinkedList()
4+
for (i in 0 until rooms.size) {
5+
for (j in 0 until rooms[0].size) {
6+
if (rooms[i][j] == 0) {
7+
queue.offer(Pair(i, j))
8+
}
9+
}
10+
}
11+
12+
val directions = arrayOf(arrayOf(-1, 0), arrayOf(1, 0), arrayOf(0, -1), arrayOf(0, 1))
13+
var distance = 1
14+
while (queue.isNotEmpty()) {
15+
val size = queue.size
16+
repeat(size) {
17+
val cell = queue.poll()
18+
for (direction in directions) {
19+
val newRow = cell.first+direction[0]
20+
val newCol = cell.second+direction[1]
21+
if (newRow >= 0 && newRow < rooms.size && newCol >= 0 && newCol < rooms[0].size && rooms[newRow][newCol] == Integer.MAX_VALUE) {
22+
rooms[newRow][newCol] = distance
23+
queue.offer(Pair(newRow, newCol))
24+
}
25+
}
26+
}
27+
distance++
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)