Skip to content

Commit e90e67f

Browse files
authored
Merge pull request #1077 from gwbaik9717/main
[ganu] Week 13
2 parents 0c3ddd9 + 7d3cc69 commit e90e67f

File tree

4 files changed

+345
-0
lines changed

4 files changed

+345
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// last test case failed
2+
// Time complexity: O(logn)
3+
// Space complexity: O(n)
4+
5+
class MinHeap {
6+
constructor() {
7+
this.heap = [null];
8+
}
9+
10+
get root() {
11+
if (this.length === 1) {
12+
return null;
13+
}
14+
15+
return this.heap[1];
16+
}
17+
18+
get length() {
19+
return this.heap.length;
20+
}
21+
22+
push(value) {
23+
this.heap.push(value);
24+
25+
let current = this.heap.length - 1;
26+
let parent = Math.floor(current / 2);
27+
28+
while (parent && this.heap[current] < this.heap[parent]) {
29+
[this.heap[current], this.heap[parent]] = [
30+
this.heap[parent],
31+
this.heap[current],
32+
];
33+
current = parent;
34+
parent = Math.floor(current / 2);
35+
}
36+
}
37+
38+
pop() {
39+
if (this.heap.length === 1) {
40+
return null;
41+
}
42+
43+
if (this.heap.length === 2) {
44+
return this.heap.pop();
45+
}
46+
47+
const rv = this.heap[1];
48+
this.heap[1] = this.heap.pop();
49+
50+
let current = 1;
51+
let left = current * 2;
52+
let right = left + 1;
53+
54+
while (
55+
(this.heap[left] && this.heap[current] > this.heap[left]) ||
56+
(this.heap[right] && this.heap[current] > this.heap[right])
57+
) {
58+
if (this.heap[right] && this.heap[right] < this.heap[left]) {
59+
[this.heap[right], this.heap[current]] = [
60+
this.heap[current],
61+
this.heap[right],
62+
];
63+
current = right;
64+
} else {
65+
[this.heap[left], this.heap[current]] = [
66+
this.heap[current],
67+
this.heap[left],
68+
];
69+
current = left;
70+
}
71+
72+
left = current * 2;
73+
right = left + 1;
74+
}
75+
76+
return rv;
77+
}
78+
}
79+
80+
var MedianFinder = function () {
81+
this.leftMinHeap = new MinHeap();
82+
this.rightMinHeap = new MinHeap();
83+
};
84+
85+
/**
86+
* @param {number} num
87+
* @return {void}
88+
*/
89+
MedianFinder.prototype.addNum = function (num) {
90+
const rightMinValue = this.rightMinHeap.root;
91+
92+
if (num >= rightMinValue) {
93+
this.rightMinHeap.push(num);
94+
} else {
95+
this.leftMinHeap.push(num * -1);
96+
}
97+
98+
if (this.rightMinHeap.length - this.leftMinHeap.length > 1) {
99+
const popped = this.rightMinHeap.pop();
100+
this.leftMinHeap.push(popped * -1);
101+
}
102+
103+
if (this.leftMinHeap.length - this.rightMinHeap.length > 1) {
104+
const popped = this.leftMinHeap.pop();
105+
this.rightMinHeap.push(popped * -1);
106+
}
107+
};
108+
109+
/**
110+
* @return {number}
111+
*/
112+
MedianFinder.prototype.findMedian = function () {
113+
const len = this.leftMinHeap.length + this.rightMinHeap.length;
114+
115+
if (len % 2 === 0) {
116+
return (this.leftMinHeap.root * -1 + this.rightMinHeap.root) / 2;
117+
}
118+
119+
if (this.leftMinHeap.length > this.rightMinHeap.length) {
120+
return this.leftMinHeap.root * -1;
121+
}
122+
123+
return this.rightMinHeap.root;
124+
};
125+
126+
/**
127+
* Your MedianFinder object will be instantiated and called as such:
128+
* var obj = new MedianFinder()
129+
* obj.addNum(num)
130+
* var param_2 = obj.findMedian()
131+
*/

insert-interval/gwbaik9717.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[][]} intervals
6+
* @param {number[]} newInterval
7+
* @return {number[][]}
8+
*/
9+
var insert = function (intervals, newInterval) {
10+
// 1. Insert newInterval
11+
const candidates = [];
12+
let inserted = false;
13+
14+
if (intervals.length === 0) {
15+
candidates.push(newInterval);
16+
}
17+
18+
for (const [start, end] of intervals) {
19+
const [newStart, newEnd] = newInterval;
20+
21+
if (!inserted) {
22+
if (newStart <= start) {
23+
candidates.push([newStart, newEnd]);
24+
inserted = true;
25+
}
26+
}
27+
28+
candidates.push([start, end]);
29+
}
30+
31+
if (!inserted) {
32+
candidates.push(newInterval);
33+
}
34+
35+
// 2. Merge if needed
36+
37+
const answer = [];
38+
39+
for (const [start, end] of candidates) {
40+
if (answer.length === 0) {
41+
answer.push([start, end]);
42+
continue;
43+
}
44+
45+
const [compareStart, compareEnd] = answer.at(-1);
46+
47+
if (compareEnd >= start) {
48+
answer.pop();
49+
answer.push([Math.min(start, compareStart), Math.max(end, compareEnd)]);
50+
continue;
51+
}
52+
53+
answer.push([start, end]);
54+
}
55+
56+
return answer;
57+
};
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Time complexity: O(nlogn)
2+
// Space complexity: O(n)
3+
4+
class _Queue {
5+
constructor() {
6+
this.q = [];
7+
this.front = 0;
8+
this.rear = 0;
9+
}
10+
11+
push(value) {
12+
this.q.push(value);
13+
this.rear++;
14+
}
15+
16+
shift() {
17+
const rv = this.q[this.front];
18+
delete this.q[this.front++];
19+
return rv;
20+
}
21+
22+
isEmpty() {
23+
return this.front === this.rear;
24+
}
25+
}
26+
27+
class MinHeap {
28+
constructor() {
29+
this.heap = [null];
30+
}
31+
32+
push(value) {
33+
this.heap.push(value);
34+
35+
let current = this.heap.length - 1;
36+
let parent = Math.floor(current / 2);
37+
38+
while (parent && this.heap[current] < this.heap[parent]) {
39+
[this.heap[current], this.heap[parent]] = [
40+
this.heap[parent],
41+
this.heap[current],
42+
];
43+
current = parent;
44+
parent = Math.floor(current / 2);
45+
}
46+
}
47+
48+
pop() {
49+
if (this.heap.length === 1) {
50+
return null;
51+
}
52+
53+
if (this.heap.length === 2) {
54+
return this.heap.pop();
55+
}
56+
57+
const rv = this.heap[1];
58+
this.heap[1] = this.heap.pop();
59+
60+
let current = 1;
61+
let left = current * 2;
62+
let right = left + 1;
63+
64+
while (
65+
(this.heap[left] && this.heap[current] > this.heap[left]) ||
66+
(this.heap[right] && this.heap[current] > this.heap[right])
67+
) {
68+
if (this.heap[right] && this.heap[right] < this.heap[left]) {
69+
[this.heap[right], this.heap[current]] = [
70+
this.heap[current],
71+
this.heap[right],
72+
];
73+
current = right;
74+
} else {
75+
[this.heap[left], this.heap[current]] = [
76+
this.heap[current],
77+
this.heap[left],
78+
];
79+
current = left;
80+
}
81+
82+
left = current * 2;
83+
right = left + 1;
84+
}
85+
86+
return rv;
87+
}
88+
}
89+
/**
90+
* Definition for a binary tree node.
91+
* function TreeNode(val, left, right) {
92+
* this.val = (val===undefined ? 0 : val)
93+
* this.left = (left===undefined ? null : left)
94+
* this.right = (right===undefined ? null : right)
95+
* }
96+
*/
97+
/**
98+
* @param {TreeNode} root
99+
* @param {number} k
100+
* @return {number}
101+
*/
102+
var kthSmallest = function (root, k) {
103+
const minHeap = new MinHeap();
104+
105+
const q = new _Queue();
106+
q.push(root);
107+
108+
while (!q.isEmpty()) {
109+
const current = q.shift();
110+
111+
minHeap.push(current.val);
112+
113+
if (current.left) {
114+
q.push(current.left);
115+
}
116+
117+
if (current.right) {
118+
q.push(current.right);
119+
}
120+
}
121+
122+
let answer = null;
123+
for (let i = 0; i < k; i++) {
124+
answer = minHeap.pop();
125+
}
126+
127+
return answer;
128+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// h : height of BST
2+
// Time complexity: O(h)
3+
// Space complexity: O(h)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
13+
/**
14+
* @param {TreeNode} root
15+
* @param {TreeNode} p
16+
* @param {TreeNode} q
17+
* @return {TreeNode}
18+
*/
19+
var lowestCommonAncestor = function (root, p, q) {
20+
if (root.val < p.val && root.val < q.val && root.left) {
21+
return lowestCommonAncestor(root.right, p, q);
22+
}
23+
24+
if (root.val > p.val && root.val > q.val && root.right) {
25+
return lowestCommonAncestor(root.left, p, q);
26+
}
27+
28+
return root;
29+
};

0 commit comments

Comments
 (0)