Skip to content

[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 6 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions find-median-from-data-stream/gwbaik9717.js
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()
*/
57 changes: 57 additions & 0 deletions insert-interval/gwbaik9717.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 풀이에 정성을 쏟으신게 느껴집니다...!
해당 문제에 대한 제가 생각하는 가독성 좋은 답안은 디스코드에 올려 공유하도록 하겠습니다!

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;
};
128 changes: 128 additions & 0 deletions kth-smallest-element-in-a-bst/gwbaik9717.js
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 lowest-common-ancestor-of-a-binary-search-tree/gwbaik9717.js
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;
};