Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HerrineKim] Week 7 #950

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions number-of-islands/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 시간 복잡도: O(m * n)
// 공간 복잡도: O(m * n)

/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function (grid) {
if (!grid || grid.length === 0) return 0;

const rows = grid.length;
const cols = grid[0].length;
let islandCount = 0;

const dfs = (row, col) => {
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] === '0') {
return;
}

grid[row][col] = '0';

dfs(row - 1, col);
dfs(row + 1, col);
dfs(row, col - 1);
dfs(row, col + 1);
};

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === '1') {
islandCount++;
dfs(i, j);
}
}
}

return islandCount;
};
35 changes: 35 additions & 0 deletions set-matrix-zeroes/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 시간 복잡도: O(m * n)
// 공간 복잡도: O(m + n)

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
if (!matrix || matrix.length === 0) return [];

const rows = matrix.length;
const cols = matrix[0].length;

const zeroRows = new Array(rows).fill(false);
const zeroCols = new Array(cols).fill(false);
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도를 약간(?) 포기하는 대신에 가독성을 늘리는 쪽을 개인적으로는 이쪽을 더 선호하는데 참고가 되었습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다! 안 그래도 문제에 공간 복잡도를 더 줄일 수 있다고 적혀 있는데 잘 생각이 안나더라구요 ^^; 즐거운 연휴 보내세요!


for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
if (matrix[row][col] === 0) {
zeroRows[row] = true;
zeroCols[col] = true;
}
}
}

for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
if (zeroRows[row] || zeroCols[col]) {
matrix[row][col] = 0;
}
}
}

return matrix;
};
Loading