-
-
Notifications
You must be signed in to change notification settings - Fork 195
[선재] WEEK07 문제풀이 #493
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
Changes from 5 commits
d819cc5
f8fa89a
4755985
d6e3890
938f415
10d40b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @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; | ||
let end = 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); | ||
end += 1; | ||
answer = Math.max(answer, end - start); | ||
} | ||
|
||
return answer; | ||
}; |
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; | ||
}; |
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 search = (target) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 오 reverseList 단어가 들어가니 더 명시적이네요! 변수명이 제일어렵네요 😂 |
||
if (target === null) return; | ||
|
||
const node = new ListNode(target.val, answer); | ||
answer = node; | ||
|
||
search(target.next); | ||
}; | ||
|
||
search(head); | ||
|
||
return answer; | ||
}; |
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() }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 맞아요. 동일한 크기인 matrix를 만들어서 memo를 활용하는 방법도 가능하나, stack이나 queue와 같이 0인지점을 저장하고 모두 소모하는 방식이 조금이라도 공간을 덜 사용하지 않을까 싶어서 선택했어요. |
||
|
||
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; | ||
}; |
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]; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.