-
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
[선재] WEEK07 문제풀이 #493
Merged
Merged
[선재] WEEK07 문제풀이 #493
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d819cc5
2. Longest Substring Without Repeating Characters
Sunjae95 f8fa89a
3. Number of Islands
Sunjae95 4755985
4. Unique Paths
Sunjae95 d6e3890
5. Set Matrix Zeroes
Sunjae95 938f415
1. Reverse Linked List
Sunjae95 10d40b1
리뷰반영
Sunjae95 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
22 changes: 22 additions & 0 deletions
22
longest-substring-without-repeating-characters/sunjae95.js
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,22 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* hash table + two pointer | ||
* | ||
* n = length of s | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var lengthOfLongestSubstring = function (s) { | ||
const map = new Map(); | ||
let answer = 0; | ||
let start = 0; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start); | ||
map.set(s[i], i); | ||
answer = Math.max(answer, i - start + 1); | ||
} | ||
|
||
return answer; | ||
}; |
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,49 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* hash table + two pointer | ||
* | ||
* n = length of grid | ||
* k = length of grid[index] | ||
* time complexity: O(n * k) | ||
* space complexity: O(n * k) | ||
*/ | ||
var numIslands = function (grid) { | ||
let answer = 0; | ||
const visited = Array.from({ length: grid.length }, (_, i) => | ||
Array.from({ length: grid[i].length }, () => false) | ||
); | ||
|
||
const dfs = (r, c) => { | ||
const dr = [0, 1, 0, -1]; | ||
const dc = [1, 0, -1, 0]; | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const nextR = r + dr[i]; | ||
const nextC = c + dc[i]; | ||
|
||
if ( | ||
nextR >= 0 && | ||
nextR < grid.length && | ||
nextC >= 0 && | ||
nextC < grid[r].length && | ||
grid[nextR][nextC] == 1 && | ||
!visited[nextR][nextC] | ||
) { | ||
visited[nextR][nextC] = true; | ||
dfs(nextR, nextC); | ||
} | ||
} | ||
}; | ||
|
||
for (let row = 0; row < grid.length; row++) { | ||
for (let column = 0; column < grid[row].length; column++) { | ||
if (grid[row][column] == 1 && !visited[row][column]) { | ||
visited[row][column] = true; | ||
answer++; | ||
dfs(row, column); | ||
} | ||
} | ||
} | ||
return answer; | ||
}; |
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,25 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* Thinking of stacking nodes like stacks while traveling | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
|
||
var reverseList = function (head) { | ||
let answer = null; | ||
|
||
const buildReverseList = (target) => { | ||
if (target === null) return; | ||
|
||
const node = new ListNode(target.val, answer); | ||
answer = node; | ||
|
||
buildReverseList(target.next); | ||
}; | ||
|
||
buildReverseList(head); | ||
|
||
return answer; | ||
}; |
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,46 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* memoization | ||
* | ||
* m: length of matrix | ||
* n: length of matrix[i] | ||
* time complexity: O(m * n) | ||
* space complexity: O(m * n) | ||
*/ | ||
var setZeroes = function (matrix) { | ||
const stack = []; | ||
const memo = { row: new Set(), column: new Set() }; | ||
|
||
const setZero = ({ r, c, isRow, isColumn }) => { | ||
const length = isRow ? matrix.length : matrix[0].length; | ||
|
||
for (let i = 0; i < length; i++) { | ||
const row = isRow ? i : r; | ||
const column = isColumn ? i : c; | ||
matrix[row][column] = 0; | ||
} | ||
}; | ||
|
||
matrix.forEach((row, r) => { | ||
row.forEach((value, c) => { | ||
if (value === 0) stack.push([r, c]); | ||
}); | ||
}); | ||
|
||
while (stack.length) { | ||
const [r, c] = stack.pop(); | ||
|
||
if (!memo.row.has(r)) { | ||
setZero({ r, c, isColumn: true }); | ||
memo.row.add(r); | ||
} | ||
|
||
if (!memo.column.has(c)) { | ||
setZero({ r, c, isRow: true }); | ||
memo.column.add(c); | ||
} | ||
} | ||
|
||
return matrix; | ||
}; |
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,25 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. dfs -> time limited | ||
* 2. dynamic programming | ||
* | ||
* time complexity: O(m * n) | ||
* space complexity: O(m * n) | ||
*/ | ||
|
||
var uniquePaths = function (m, n) { | ||
// initialize | ||
const dp = Array.from({ length: m }, (_, i) => | ||
Array.from({ length: n }, (_, j) => (i === 0 || j === 0 ? 1 : 0)) | ||
); | ||
|
||
for (let i = 1; i < m; i++) { | ||
for (let j = 1; j < n; j++) { | ||
// recurrence relation | ||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; | ||
} | ||
} | ||
|
||
return dp[m - 1][n - 1]; | ||
}; |
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.
stack이나 memo중 하나만 사용하셨어도 되지 않았을까요? 최적화를 위해 두 가지를 모두 사용하신걸까요?
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.
맞아요. 동일한 크기인 matrix를 만들어서 memo를 활용하는 방법도 가능하나, stack이나 queue와 같이 0인지점을 저장하고 모두 소모하는 방식이 조금이라도 공간을 덜 사용하지 않을까 싶어서 선택했어요.