Skip to content

Commit 2adcbc5

Browse files
committed
Number of Islands Solution
1 parent 97a33f4 commit 2adcbc5

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* ์„ฌ์˜ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ - ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฌธ์ œ
3+
* 2์ฐจ์› ๋ฐ”์ด๋„ˆ๋ฆฌ ๊ทธ๋ฆฌ๋“œ, 1์€ ์„ฌ, 0์€ ๋ฌผ
4+
* ๊ทธ๋ฆฌ๋“œ์˜ ๋ชจ๋“  ๊ฐ€์žฅ์ž๋ฆฌ๋Š” ๋ฌผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์Œ
5+
*
6+
* ๋ฌธ์ œ ์ ‘๊ทผ: BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์‚ฌ์šฉ
7+
* 1. ๊ทธ๋ฆฌ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋•…(1)์„ ๋ฐœ๊ฒฌํ•˜๋ฉด BFS๋กœ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๋•…์„ ํƒ์ƒ‰
8+
* 2. BFS ํƒ์ƒ‰ ์ค‘์— ๋ฐฉ๋ฌธํ•œ ๋•…์€ ๋ฐฉ๋ฌธ ๋ฐฐ์—ด์— ํ‘œ์‹œ
9+
* 3. BFS ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด ์„ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฆ๊ฐ€
10+
* 4. ๊ทธ๋ฆฌ๋“œ ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์„ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ
11+
*
12+
* BFS ์„ ํƒ ์ด์œ : ์ œ์•ฝ ์กฐ๊ฑด(๊ทธ๋ฆฌ๋“œ ํฌ๊ธฐ ์ตœ๋Œ€ 300x300)์„ ๊ณ ๋ คํ–ˆ์„ ๋•Œ,
13+
* - ๊ทธ๋ฆฌ๋“œ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก DFS๋Š” ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ์ธํ•œ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์œ„ํ—˜์ด ์žˆ์Œ,
14+
* - BFS๋Š” ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์œ„ํ—˜์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Œ
15+
* - ๋ฐฉ๋ฌธ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์›๋ณธ๋ฐ์ดํ„ฐ ๋ณด์กด(๋ถˆ๋ณ€์„ฑ ์œ ์ง€)
16+
* - ๋‹จ, BFS๋Š” ํ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋” ๋งŽ์„ ์ˆ˜ ์žˆ์Œ
17+
*
18+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(Mร—N) (M: ํ–‰์˜ ๊ฐœ์ˆ˜, N: ์—ด์˜ ๊ฐœ์ˆ˜)
19+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(Mร—N) (๋ฐฉ๋ฌธ ๋ฐฐ์—ด๊ณผ ํ๋ฅผ ์‚ฌ์šฉ)
20+
*/
21+
/**
22+
* @param {character[][]} grid
23+
* @return {number}
24+
*/
25+
var numIslands = function (grid) {
26+
if (!grid || grid.length === 0) {
27+
return 0;
28+
}
29+
30+
const rows = grid.length;
31+
const cols = grid[0].length;
32+
let islandCount = 0;
33+
34+
// ๋ฐฉ๋ฌธ ๋ฐฐ์—ด ์ƒ์„ฑ
35+
const visited = Array(rows)
36+
.fill()
37+
.map(() => Array(cols).fill(false));
38+
39+
// ๋ฐฉํ–ฅ ๋ฐฐ์—ด (์ƒ, ํ•˜, ์ขŒ, ์šฐ)
40+
const directions = [
41+
[-1, 0],
42+
[1, 0],
43+
[0, -1],
44+
[0, 1],
45+
];
46+
47+
function bfs(startRow, startCol) {
48+
const queue = [[startRow, startCol]];
49+
visited[startRow][startCol] = true; // ์‹œ์ž‘์  ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
50+
51+
while (queue.length > 0) {
52+
const [row, col] = queue.shift();
53+
54+
// 4๋ฐฉํ–ฅ ํƒ์ƒ‰
55+
for (const [dr, dc] of directions) {
56+
const newRow = row + dr;
57+
const newCol = col + dc;
58+
59+
// ๋ฒ”์œ„ ์•ˆ์—์„œ, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋•…('1')์ด๋ฉด ํƒ์ƒ‰
60+
if (
61+
newRow >= 0 &&
62+
newRow < rows &&
63+
newCol >= 0 &&
64+
newCol < cols &&
65+
grid[newRow][newCol] === '1' &&
66+
!visited[newRow][newCol]
67+
) {
68+
queue.push([newRow, newCol]);
69+
visited[newRow][newCol] = true; // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
70+
}
71+
}
72+
}
73+
}
74+
75+
// ๊ทธ๋ฆฌ๋“œ ์ „์ฒด ์ˆœํšŒ
76+
for (let i = 0; i < rows; i++) {
77+
for (let j = 0; j < cols; j++) {
78+
// ๋•…('1')์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด BFS ์‹œ์ž‘ ๋ฐ ์„ฌ ์นด์šดํŠธ ์ฆ๊ฐ€
79+
if (grid[i][j] === '1' && !visited[i][j]) {
80+
islandCount++;
81+
bfs(i, j);
82+
}
83+
}
84+
}
85+
86+
return islandCount;
87+
};

0 commit comments

Comments
ย (0)