Skip to content

Commit 78c4ea8

Browse files
committed
[Week7](gmlwls96) Number of Islands
1 parent 16fb71d commit 78c4ea8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

longest-substring-without-repeating-characters/gmlwls96.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Solution {
1111
subStr.append(it)
1212
}
1313
max = max(max, subStr.length)
14-
println(subStr)
1514
return max
1615
}
1716
}

number-of-islands/gmlwls96.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
// 시간 : O(N), 공간 O(N)
3+
fun numIslands(grid: Array<CharArray>): Int {
4+
val moveMap = arrayOf(
5+
intArrayOf(1, 0),
6+
intArrayOf(0, 1)
7+
)
8+
val queue = mutableListOf<IntArray>()
9+
val visitMap = Array(grid.size) {
10+
BooleanArray(grid[it].size)
11+
}
12+
var count = 0
13+
// 1. grid를 모두 조회
14+
for (y in grid.indices) {
15+
for (x in grid[y].indices) {
16+
// 2. grid[y][x]가 1이고 방문한적이 없으면 newLand로 인지하고 count를 올린다.
17+
if (grid[y][x] == '1' && !visitMap[y][x]) {
18+
count++
19+
queue.add(intArrayOf(y, x))
20+
}
21+
// 3. 인접해있는 land를 조회하기위해 queue를 이용하여 탐색하고, visitMap을 변경해준다.
22+
while (queue.isNotEmpty()) {
23+
val newInfo = queue.removeFirst()
24+
val newY = newInfo[0]
25+
val newX = newInfo[1]
26+
visitMap[newY][newX] = true
27+
moveMap.forEach {
28+
val nextY = newY + it[0]
29+
val nextX = newX + it[1]
30+
if (nextY < grid.size
31+
&& nextX < grid[nextY].size
32+
&& grid[nextY][nextX] == '1'
33+
&& !visitMap[nextY][nextX]
34+
) {
35+
queue.add(intArrayOf(nextY, nextX))
36+
}
37+
}
38+
}
39+
}
40+
}
41+
return count
42+
}
43+
}

0 commit comments

Comments
 (0)