-
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
[ganu] Week 10 #1008
Merged
Merged
[ganu] Week 10 #1008
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ffc9a0
feat: 226. Invert Binary Tree
gwbaik9717 322b54d
feat: 33. Search in Rotated Sorted Array
gwbaik9717 aca3c7d
feat: 55. Jump Game
gwbaik9717 a69c944
feat: 207. Course Schedule
gwbaik9717 27fac22
feat: 23. Merge k Sorted Lists
gwbaik9717 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,68 @@ | ||
// n: numCourses, p: len(prerequisites) | ||
// Time complexity: O(n + p) | ||
// Space complexity: O(n + p) | ||
|
||
class _Queue { | ||
constructor() { | ||
this.q = []; | ||
this.start = 0; | ||
this.end = 0; | ||
} | ||
|
||
isEmpty() { | ||
return this.start === this.end; | ||
} | ||
|
||
push(value) { | ||
this.q.push(value); | ||
this.end++; | ||
} | ||
|
||
shift() { | ||
const rv = this.q[this.start]; | ||
delete this.q[this.start++]; | ||
|
||
return rv; | ||
} | ||
} | ||
|
||
/** | ||
* @param {number} numCourses | ||
* @param {number[][]} prerequisites | ||
* @return {boolean} | ||
*/ | ||
var canFinish = function (numCourses, prerequisites) { | ||
const graph = Array.from({ length: numCourses }, () => []); | ||
const inDegree = Array.from({ length: numCourses }, () => 0); | ||
|
||
for (const [end, start] of prerequisites) { | ||
graph[start].push(end); | ||
inDegree[end]++; | ||
} | ||
|
||
const q = new _Queue(); | ||
|
||
for (let i = 0; i < numCourses; i++) { | ||
if (inDegree[i] === 0) { | ||
q.push(i); | ||
} | ||
} | ||
|
||
let count = 0; | ||
|
||
while (!q.isEmpty()) { | ||
const current = q.shift(); | ||
count++; | ||
|
||
// 현재 노드와 연결된 다른 노드의 차수 감소 | ||
for (const node of graph[current]) { | ||
inDegree[node] -= 1; | ||
|
||
if (inDegree[node] === 0) { | ||
q.push(node); | ||
} | ||
} | ||
} | ||
|
||
return count === numCourses; | ||
}; |
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,33 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {TreeNode} | ||
*/ | ||
var invertTree = function (root) { | ||
const dfs = (current) => { | ||
if (!current) { | ||
return; | ||
} | ||
|
||
const temp = current.left; | ||
current.left = current.right; | ||
current.right = temp; | ||
|
||
dfs(current.left); | ||
dfs(current.right); | ||
}; | ||
|
||
dfs(root); | ||
|
||
return root; | ||
}; | ||
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,28 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
Comment on lines
+1
to
+2
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. 저도 거의 동일한 방식으로 풀었습니다! 표현하는 방식 등 여러가지로 파이썬과 다른 부분이 있어서 비교해서 보는 재미가 있었어요. 시간/공간 복잡도에 대한 설명을 추가해주시면 더 좋을 것 같습니다. 이번 주도 고생하셨습니다! |
||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canJump = function (nums) { | ||
let maxValue = nums[0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
if (i > maxValue) { | ||
return false; | ||
} | ||
|
||
const newNum = i + nums[i]; | ||
|
||
if (newNum > maxValue) { | ||
maxValue = newNum; | ||
} | ||
|
||
if (maxValue >= nums.length - 1) { | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
}; |
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,134 @@ | ||
// k: len(lists), n: Total number of nodes | ||
// Time complexity: O(nlogk) | ||
// Space complexity: O(n) | ||
|
||
class MinHeap { | ||
constructor() { | ||
this.heap = [null]; | ||
} | ||
|
||
isEmpty() { | ||
return this.heap.length === 1; | ||
} | ||
|
||
push(listNode) { | ||
this.heap.push(listNode); | ||
|
||
let current = this.heap.length - 1; | ||
let parent = Math.floor(current / 2); | ||
|
||
while (parent !== 0) { | ||
if (this.heap[parent].val > listNode.val) { | ||
[this.heap[parent], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[parent], | ||
]; | ||
|
||
current = parent; | ||
parent = Math.floor(current / 2); | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
pop() { | ||
if (this.heap.length === 1) { | ||
return; | ||
} | ||
|
||
if (this.heap.length === 2) { | ||
return this.heap.pop(); | ||
} | ||
|
||
const rv = this.heap[1]; | ||
|
||
this.heap[1] = this.heap.pop(); | ||
|
||
let current = 1; | ||
let left = current * 2; | ||
let right = left + 1; | ||
|
||
while ( | ||
(this.heap[left] && this.heap[current].val > this.heap[left].val) || | ||
(this.heap[right] && this.heap[current].val > this.heap[right].val) | ||
) { | ||
if (this.heap[left] && this.heap[right]) { | ||
if (this.heap[left].val < this.heap[right].val) { | ||
[this.heap[left], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[left], | ||
]; | ||
current = left; | ||
} else { | ||
[this.heap[right], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[right], | ||
]; | ||
current = right; | ||
} | ||
|
||
left = current * 2; | ||
right = left + 1; | ||
continue; | ||
} | ||
|
||
[this.heap[left], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[left], | ||
]; | ||
current = left; | ||
left = current * 2; | ||
right = left + 1; | ||
} | ||
|
||
return rv; | ||
} | ||
} | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode[]} lists | ||
* @return {ListNode} | ||
*/ | ||
var mergeKLists = function (lists) { | ||
const minHeap = new MinHeap(); | ||
|
||
for (const list of lists) { | ||
if (list) { | ||
minHeap.push(list); | ||
} | ||
} | ||
|
||
let answer = null; | ||
let next = null; | ||
|
||
while (!minHeap.isEmpty()) { | ||
const current = minHeap.pop(); | ||
|
||
if (!answer) { | ||
answer = new ListNode(); | ||
next = answer; | ||
} | ||
|
||
next.val = current.val; | ||
|
||
if (current.next) { | ||
minHeap.push(current.next); | ||
} | ||
|
||
if (!minHeap.isEmpty()) { | ||
next.next = new ListNode(); | ||
next = next.next; | ||
} | ||
} | ||
|
||
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,40 @@ | ||
// Time complexity: O(logn) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var search = function (nums, target) { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
const mid = Math.floor((left + right) / 2); | ||
|
||
if (nums.at(mid) === target) { | ||
return mid; | ||
} | ||
|
||
// rotate 된 구간이 있을 때 | ||
if (nums.at(mid + 1) > nums.at(right)) { | ||
if (nums.at(right) >= target || nums.at(mid + 1) <= target) { | ||
left = mid + 1; | ||
continue; | ||
} | ||
|
||
right = mid - 1; | ||
continue; | ||
} | ||
|
||
if (target >= nums.at(mid + 1) && target <= nums.at(right)) { | ||
left = mid + 1; | ||
continue; | ||
} | ||
|
||
right = mid - 1; | ||
} | ||
|
||
return -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.
DFS와 재귀를 활용하면 더 깔끔한 코드로 구현할 수 있겠어요. 참고가 되었습니다. 감사합니다!