-
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
[선재] WEEK10 Solutions #537
Changes from all commits
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,47 @@ | ||
/** | ||
* @description | ||
* memoization + dfs | ||
* | ||
* n = length of nums | ||
* p = length of prerequisites | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(p) | ||
*/ | ||
var canFinish = function (numCourses, prerequisites) { | ||
const memo = Array.from({ length: numCourses + 1 }, () => false); | ||
const visited = Array.from({ length: numCourses + 1 }, () => false); | ||
// graph setting | ||
const graph = prerequisites.reduce((map, [linkedNode, current]) => { | ||
const list = map.get(current) ?? []; | ||
list.push(linkedNode); | ||
map.set(current, list); | ||
return map; | ||
}, new Map()); | ||
|
||
const dfs = (current) => { | ||
const linkedNode = graph.get(current); | ||
|
||
if (memo[current] || !linkedNode || linkedNode.length === 0) return true; | ||
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. 오 그렇네요🙌 |
||
|
||
for (const node of linkedNode) { | ||
if (visited[node]) return false; | ||
|
||
visited[node] = true; | ||
if (!dfs(node)) return false; | ||
visited[node] = false; | ||
memo[node] = true; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
for (const [current] of graph) { | ||
visited[current] = true; | ||
if (!dfs(current)) return false; | ||
visited[current] = false; | ||
memo[current] = true; | ||
} | ||
|
||
return true; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* preorder traverse | ||
* | ||
* n = length of root | ||
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. length of root는 전체 노드의 수를 말씀하시는걸까요? 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. 혹시 |
||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var invertTree = function (root) { | ||
const preOrder = (tree) => { | ||
if (tree === null) return null; | ||
|
||
const currentNode = new TreeNode(tree.val); | ||
|
||
currentNode.right = preOrder(tree.left); | ||
currentNode.left = preOrder(tree.right); | ||
|
||
return currentNode; | ||
}; | ||
|
||
return preOrder(root); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @description | ||
* | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var canJump = function (nums) { | ||
let cur = 1; | ||
for (const num of nums) { | ||
if (cur === 0) return false; | ||
cur--; | ||
cur = cur > num ? cur : num; | ||
} | ||
|
||
return true; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @description | ||
* queue의 특성을 활용하여 풀이 | ||
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. 이 부분에 대해서 혹시 추가 설명을 요청드려도 될까요? |
||
* | ||
* n = length of lists | ||
* m = length of lists[i] | ||
* time complexity: O(n * n * m) | ||
* space complexity: O(n*m) | ||
*/ | ||
var mergeKLists = function (lists) { | ||
let answer = null; | ||
let tail = null; | ||
let totalSize = lists.reduce((size, list) => { | ||
let head = list; | ||
let count = 0; | ||
|
||
while (head) { | ||
head = head.next; | ||
count++; | ||
} | ||
|
||
return size + count; | ||
}, 0); | ||
|
||
while (totalSize--) { | ||
let minIndex = lists.reduce((acc, list, i) => { | ||
if (list === null) return acc; | ||
if (acc === null) return { value: list.val, index: i }; | ||
return acc.value < list.val ? acc : { value: list.val, index: i }; | ||
}, null).index; | ||
|
||
if (answer === null) { | ||
answer = lists[minIndex]; | ||
tail = answer; | ||
} else { | ||
tail.next = lists[minIndex]; | ||
tail = lists[minIndex]; | ||
} | ||
Comment on lines
+32
to
+38
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. @HC-kang |
||
|
||
lists[minIndex] = lists[minIndex].next; | ||
} | ||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @description | ||
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var search = function (nums, target) { | ||
let [start, end] = [0, nums.length - 1]; | ||
let answer = -1; | ||
|
||
while (start !== end) { | ||
if (nums[start] === target) answer = start; | ||
if (nums[end] === target) answer = end; | ||
if (nums[start] > nums[end]) end--; | ||
else start++; | ||
} | ||
|
||
if (nums[start] === target) answer = start; | ||
|
||
return answer; | ||
}; |
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.
와 처음에는 메모가 과연 효과가 있을까..? 생각했는데 몇번을 돌려도 Beats 100%네요 👍