Skip to content

Commit 5ce2b69

Browse files
authored
Merge pull request #950 from HerrineKim/main
[HerrineKim] Week 7
2 parents 21f4334 + c639f7c commit 5ce2b69

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

number-of-islands/HerrineKim.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 시간 복잡도: O(m * n)
2+
// 공간 복잡도: O(m * n)
3+
4+
/**
5+
* @param {character[][]} grid
6+
* @return {number}
7+
*/
8+
var numIslands = function (grid) {
9+
if (!grid || grid.length === 0) return 0;
10+
11+
const rows = grid.length;
12+
const cols = grid[0].length;
13+
let islandCount = 0;
14+
15+
const dfs = (row, col) => {
16+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] === '0') {
17+
return;
18+
}
19+
20+
grid[row][col] = '0';
21+
22+
dfs(row - 1, col);
23+
dfs(row + 1, col);
24+
dfs(row, col - 1);
25+
dfs(row, col + 1);
26+
};
27+
28+
for (let i = 0; i < rows; i++) {
29+
for (let j = 0; j < cols; j++) {
30+
if (grid[i][j] === '1') {
31+
islandCount++;
32+
dfs(i, j);
33+
}
34+
}
35+
}
36+
37+
return islandCount;
38+
};

set-matrix-zeroes/HerrineKim.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 시간 복잡도: O(m * n)
2+
// 공간 복잡도: O(m + n)
3+
4+
/**
5+
* @param {number[][]} matrix
6+
* @return {void} Do not return anything, modify matrix in-place instead.
7+
*/
8+
var setZeroes = function (matrix) {
9+
if (!matrix || matrix.length === 0) return [];
10+
11+
const rows = matrix.length;
12+
const cols = matrix[0].length;
13+
14+
const zeroRows = new Array(rows).fill(false);
15+
const zeroCols = new Array(cols).fill(false);
16+
17+
for (let row = 0; row < rows; row++) {
18+
for (let col = 0; col < cols; col++) {
19+
if (matrix[row][col] === 0) {
20+
zeroRows[row] = true;
21+
zeroCols[col] = true;
22+
}
23+
}
24+
}
25+
26+
for (let row = 0; row < rows; row++) {
27+
for (let col = 0; col < cols; col++) {
28+
if (zeroRows[row] || zeroCols[col]) {
29+
matrix[row][col] = 0;
30+
}
31+
}
32+
}
33+
34+
return matrix;
35+
};

0 commit comments

Comments
 (0)