-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week 13 #1077
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
Merged
[ganu] Week 13 #1077
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2d7183
feat: 230. Kth Smallest Element in a BST
gwbaik9717 d8d466f
feat: inset-interval
gwbaik9717 631f5e4
feat: Lowest Common Ancestor of a Binary Search Tree
gwbaik9717 689582d
refactor: Lowest Common Ancestor of a Binary Search Tree
gwbaik9717 fff39b4
feat: Find Median From Data Stream
gwbaik9717 7d3cc69
refactor: Find Median From Data Stream
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 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,131 @@ | ||
// last test case failed | ||
// Time complexity: O(logn) | ||
// Space complexity: O(n) | ||
|
||
class MinHeap { | ||
constructor() { | ||
this.heap = [null]; | ||
} | ||
|
||
get root() { | ||
if (this.length === 1) { | ||
return null; | ||
} | ||
|
||
return this.heap[1]; | ||
} | ||
|
||
get length() { | ||
return this.heap.length; | ||
} | ||
|
||
push(value) { | ||
this.heap.push(value); | ||
|
||
let current = this.heap.length - 1; | ||
let parent = Math.floor(current / 2); | ||
|
||
while (parent && this.heap[current] < this.heap[parent]) { | ||
[this.heap[current], this.heap[parent]] = [ | ||
this.heap[parent], | ||
this.heap[current], | ||
]; | ||
current = parent; | ||
parent = Math.floor(current / 2); | ||
} | ||
} | ||
|
||
pop() { | ||
if (this.heap.length === 1) { | ||
return null; | ||
} | ||
|
||
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] > this.heap[left]) || | ||
(this.heap[right] && this.heap[current] > this.heap[right]) | ||
) { | ||
if (this.heap[right] && this.heap[right] < this.heap[left]) { | ||
[this.heap[right], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[right], | ||
]; | ||
current = right; | ||
} else { | ||
[this.heap[left], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[left], | ||
]; | ||
current = left; | ||
} | ||
|
||
left = current * 2; | ||
right = left + 1; | ||
} | ||
|
||
return rv; | ||
} | ||
} | ||
|
||
var MedianFinder = function () { | ||
this.leftMinHeap = new MinHeap(); | ||
this.rightMinHeap = new MinHeap(); | ||
}; | ||
|
||
/** | ||
* @param {number} num | ||
* @return {void} | ||
*/ | ||
MedianFinder.prototype.addNum = function (num) { | ||
const rightMinValue = this.rightMinHeap.root; | ||
|
||
if (num >= rightMinValue) { | ||
this.rightMinHeap.push(num); | ||
} else { | ||
this.leftMinHeap.push(num * -1); | ||
} | ||
|
||
if (this.rightMinHeap.length - this.leftMinHeap.length > 1) { | ||
const popped = this.rightMinHeap.pop(); | ||
this.leftMinHeap.push(popped * -1); | ||
} | ||
|
||
if (this.leftMinHeap.length - this.rightMinHeap.length > 1) { | ||
const popped = this.leftMinHeap.pop(); | ||
this.rightMinHeap.push(popped * -1); | ||
} | ||
}; | ||
|
||
/** | ||
* @return {number} | ||
*/ | ||
MedianFinder.prototype.findMedian = function () { | ||
const len = this.leftMinHeap.length + this.rightMinHeap.length; | ||
|
||
if (len % 2 === 0) { | ||
return (this.leftMinHeap.root * -1 + this.rightMinHeap.root) / 2; | ||
} | ||
|
||
if (this.leftMinHeap.length > this.rightMinHeap.length) { | ||
return this.leftMinHeap.root * -1; | ||
} | ||
|
||
return this.rightMinHeap.root; | ||
}; | ||
|
||
/** | ||
* Your MedianFinder object will be instantiated and called as such: | ||
* var obj = new MedianFinder() | ||
* obj.addNum(num) | ||
* var param_2 = obj.findMedian() | ||
*/ |
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,57 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @param {number[]} newInterval | ||
* @return {number[][]} | ||
*/ | ||
var insert = function (intervals, newInterval) { | ||
// 1. Insert newInterval | ||
const candidates = []; | ||
let inserted = false; | ||
|
||
if (intervals.length === 0) { | ||
candidates.push(newInterval); | ||
} | ||
|
||
for (const [start, end] of intervals) { | ||
const [newStart, newEnd] = newInterval; | ||
|
||
if (!inserted) { | ||
if (newStart <= start) { | ||
candidates.push([newStart, newEnd]); | ||
inserted = true; | ||
} | ||
} | ||
|
||
candidates.push([start, end]); | ||
} | ||
|
||
if (!inserted) { | ||
candidates.push(newInterval); | ||
} | ||
|
||
// 2. Merge if needed | ||
|
||
const answer = []; | ||
|
||
for (const [start, end] of candidates) { | ||
if (answer.length === 0) { | ||
answer.push([start, end]); | ||
continue; | ||
} | ||
|
||
const [compareStart, compareEnd] = answer.at(-1); | ||
|
||
if (compareEnd >= start) { | ||
answer.pop(); | ||
answer.push([Math.min(start, compareStart), Math.max(end, compareEnd)]); | ||
continue; | ||
} | ||
|
||
answer.push([start, end]); | ||
} | ||
|
||
return answer; | ||
}; |
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,128 @@ | ||
// Time complexity: O(nlogn) | ||
// Space complexity: O(n) | ||
|
||
class _Queue { | ||
constructor() { | ||
this.q = []; | ||
this.front = 0; | ||
this.rear = 0; | ||
} | ||
|
||
push(value) { | ||
this.q.push(value); | ||
this.rear++; | ||
} | ||
|
||
shift() { | ||
const rv = this.q[this.front]; | ||
delete this.q[this.front++]; | ||
return rv; | ||
} | ||
|
||
isEmpty() { | ||
return this.front === this.rear; | ||
} | ||
} | ||
|
||
class MinHeap { | ||
constructor() { | ||
this.heap = [null]; | ||
} | ||
|
||
push(value) { | ||
this.heap.push(value); | ||
|
||
let current = this.heap.length - 1; | ||
let parent = Math.floor(current / 2); | ||
|
||
while (parent && this.heap[current] < this.heap[parent]) { | ||
[this.heap[current], this.heap[parent]] = [ | ||
this.heap[parent], | ||
this.heap[current], | ||
]; | ||
current = parent; | ||
parent = Math.floor(current / 2); | ||
} | ||
} | ||
|
||
pop() { | ||
if (this.heap.length === 1) { | ||
return null; | ||
} | ||
|
||
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] > this.heap[left]) || | ||
(this.heap[right] && this.heap[current] > this.heap[right]) | ||
) { | ||
if (this.heap[right] && this.heap[right] < this.heap[left]) { | ||
[this.heap[right], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[right], | ||
]; | ||
current = right; | ||
} else { | ||
[this.heap[left], this.heap[current]] = [ | ||
this.heap[current], | ||
this.heap[left], | ||
]; | ||
current = left; | ||
} | ||
|
||
left = current * 2; | ||
right = left + 1; | ||
} | ||
|
||
return rv; | ||
} | ||
} | ||
/** | ||
* 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 | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var kthSmallest = function (root, k) { | ||
const minHeap = new MinHeap(); | ||
|
||
const q = new _Queue(); | ||
q.push(root); | ||
|
||
while (!q.isEmpty()) { | ||
const current = q.shift(); | ||
|
||
minHeap.push(current.val); | ||
|
||
if (current.left) { | ||
q.push(current.left); | ||
} | ||
|
||
if (current.right) { | ||
q.push(current.right); | ||
} | ||
} | ||
|
||
let answer = null; | ||
for (let i = 0; i < k; i++) { | ||
answer = minHeap.pop(); | ||
} | ||
|
||
return answer; | ||
}; |
29 changes: 29 additions & 0 deletions
29
lowest-common-ancestor-of-a-binary-search-tree/gwbaik9717.js
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,29 @@ | ||
// h : height of BST | ||
// Time complexity: O(h) | ||
// Space complexity: O(h) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {TreeNode} root | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {TreeNode} | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
if (root.val < p.val && root.val < q.val && root.left) { | ||
return lowestCommonAncestor(root.right, p, q); | ||
} | ||
|
||
if (root.val > p.val && root.val > q.val && root.right) { | ||
return lowestCommonAncestor(root.left, p, q); | ||
} | ||
|
||
return root; | ||
}; |
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.
문제 풀이에 정성을 쏟으신게 느껴집니다...!
해당 문제에 대한 제가 생각하는 가독성 좋은 답안은 디스코드에 올려 공유하도록 하겠습니다!