-
Notifications
You must be signed in to change notification settings - Fork 126
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
[HerrineKim] Week 7 #950
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
공간 복잡도를 약간(?) 포기하는 대신에 가독성을 늘리는 쪽을 개인적으로는 이쪽을 더 선호하는데 참고가 되었습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다! 안 그래도 문제에 공간 복잡도를 더 줄일 수 있다고 적혀 있는데 잘 생각이 안나더라구요 ^^; 즐거운 연휴 보내세요!