-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hsskey] Week 07 Solutions #1468
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
Merged
+163
−0
Merged
Changes from all commits
Commits
Show all changes
5 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 hidden or 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,20 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var lengthOfLongestSubstring = function(s) { | ||
const charSet = new Set(); | ||
let l = 0; | ||
let res = 0; | ||
|
||
for (let r = 0; r < s.length; r++) { | ||
while (charSet.has(s[r])) { | ||
charSet.delete(s[l]); | ||
l += 1; | ||
} | ||
charSet.add(s[r]); | ||
res = Math.max(res, r - l + 1); | ||
} | ||
|
||
return res; | ||
}; |
This file contains hidden or 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,56 @@ | ||
/** | ||
* @param {character[][]} grid | ||
* @return {number} | ||
*/ | ||
var numIslands = function(grid) { | ||
if (!grid.length) return 0; | ||
|
||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
const visit = new Set(); | ||
let islands = 0; | ||
|
||
const bfs = (r, c) => { | ||
const queue = []; | ||
queue.push([r, c]); | ||
visit.add(`${r},${c}`); | ||
|
||
while (queue.length) { | ||
const [row, col] = queue.shift(); | ||
const directions = [ | ||
[1, 0], | ||
[-1, 0], | ||
[0, 1], | ||
[0, -1], | ||
]; | ||
|
||
for (const [dr, dc] of directions) { | ||
const newRow = row + dr; | ||
const newCol = col + dc; | ||
|
||
if ( | ||
newRow >= 0 && | ||
newRow < rows && | ||
newCol >= 0 && | ||
newCol < cols && | ||
grid[newRow][newCol] === '1' && | ||
!visit.has(`${newRow},${newCol}`) | ||
) { | ||
queue.push([newRow, newCol]); | ||
visit.add(`${newRow},${newCol}`); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (grid[r][c] === '1' && !visit.has(`${r},${c}`)) { | ||
bfs(r, c); | ||
islands += 1; | ||
} | ||
} | ||
} | ||
|
||
return islands; | ||
}; |
This file contains hidden or 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,25 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
*/ | ||
var reverseList = function(head) { | ||
if (head === null) return null; | ||
|
||
let newHead = head; | ||
if (head.next !== null) { | ||
newHead = reverseList(head.next); | ||
|
||
head.next.next = head; | ||
} | ||
|
||
head.next = null; | ||
|
||
return newHead; | ||
}; | ||
This file contains hidden or 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,42 @@ | ||
const setZeroes = (matrix) => { | ||
const ROWS = matrix.length; | ||
const COLS = matrix[0].length; | ||
let rowZero = false; | ||
|
||
// 1. 어떤 행과 열이 0이 되어야 하는지 기록 | ||
for (let r = 0; r < ROWS; r++) { | ||
for (let c = 0; c < COLS; c++) { | ||
if (matrix[r][c] === 0) { | ||
matrix[0][c] = 0; | ||
if (r > 0) { | ||
matrix[r][0] = 0; | ||
} else { | ||
rowZero = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 2. 첫 번째 행과 첫 번째 열을 제외한 나머지 처리 | ||
for (let r = 1; r < ROWS; r++) { | ||
for (let c = 1; c < COLS; c++) { | ||
if (matrix[0][c] === 0 || matrix[r][0] === 0) { | ||
matrix[r][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 3. 첫 번째 열 처리 | ||
if (matrix[0][0] === 0) { | ||
for (let r = 0; r < ROWS; r++) { | ||
matrix[r][0] = 0; | ||
} | ||
} | ||
|
||
// 4. 첫 번째 행 처리 | ||
if (rowZero) { | ||
for (let c = 0; c < COLS; c++) { | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
}; |
This file contains hidden or 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,20 @@ | ||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var uniquePaths = function(m, n) { | ||
let row = new Array(n).fill(1); | ||
|
||
for (let i = 0; i < m - 1; i++) { | ||
const newRow = new Array(n).fill(1); | ||
|
||
for (let j = n - 2; j >= 0; j--) { | ||
newRow[j] = newRow[j + 1] + row[j]; | ||
} | ||
|
||
row = newRow; | ||
} | ||
|
||
return row[0]; | ||
}; |
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.
작성하신 코드를 보니 재귀적으로 문제를 깔끔하게 해결하신 것 같아요! 👍
저는 아직까지도 재귀함수로 접근하는 방식이 어렵게 느껴지는데, 저도 좀 더 노력해야 할 것 같다는 생각을 해봅니다.