Skip to content

[hsskey] WEEK13 Solutions #1625

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 3 commits into from
Jun 29, 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
101 changes: 101 additions & 0 deletions find-median-from-data-stream/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class Heap {
constructor(compare) {
this.data = [];
this.compare = compare;
}

size() {
return this.data.length;
}

peek() {
return this.data[0];
}

push(val) {
this.data.push(val);
this._siftUp();
}

pop() {
const top = this.peek();
const bottom = this.data.pop();
if (this.data.length > 0) {
this.data[0] = bottom;
this._siftDown();
}
return top;
}

_siftUp() {
let i = this.data.length - 1;
const node = this.data[i];
while (i > 0) {
const parent = Math.floor((i - 1) / 2);
if (this.compare(node, this.data[parent])) {
this.data[i] = this.data[parent];
i = parent;
} else break;
}
this.data[i] = node;
}

_siftDown() {
let i = 0;
const node = this.data[0];
const length = this.data.length;

while (true) {
let left = 2 * i + 1;
let right = 2 * i + 2;
let swap = i;

if (left < length && this.compare(this.data[left], this.data[swap])) {
swap = left;
}
if (right < length && this.compare(this.data[right], this.data[swap])) {
swap = right;
}
if (swap === i) break;

this.data[i] = this.data[swap];
i = swap;
}

this.data[i] = node;
}
}

var MedianFinder = function() {
this.small = new Heap((a, b) => a > b); // max heap
this.large = new Heap((a, b) => a < b); // min heap
};

MedianFinder.prototype.addNum = function(num) {
this.small.push(num);

if (
this.small.size() > 0 &&
this.large.size() > 0 &&
this.small.peek() > this.large.peek()
) {
this.large.push(this.small.pop());
}

if (this.small.size() > this.large.size() + 1) {
this.large.push(this.small.pop());
}
if (this.large.size() > this.small.size() + 1) {
this.small.push(this.large.pop());
}
};

MedianFinder.prototype.findMedian = function() {
if (this.small.size() > this.large.size()) {
return this.small.peek();
}
if (this.large.size() > this.small.size()) {
return this.large.peek();
}
return (this.small.peek() + this.large.peek()) / 2;
};
25 changes: 25 additions & 0 deletions insert-interval/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
const res = [];

for (let i = 0; i < intervals.length; i++) {
if (newInterval[1] < intervals[i][0]) {
res.push(newInterval);
return res.concat(intervals.slice(i));
} else if (newInterval[0] > intervals[i][1]) {
res.push(intervals[i]);
} else {
newInterval = [
Math.min(newInterval[0], intervals[i][0]),
Math.max(newInterval[1], intervals[i][1])
];
}
}

res.push(newInterval);
return res;
};
36 changes: 36 additions & 0 deletions kth-smallest-element-in-a-bst/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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) {
let n = 0;
const stack = [];
let cur = root;

while (cur || stack.length > 0) {
while (cur) {
stack.push(cur);
cur = cur.left;
}

cur = stack.pop();
n += 1;
if (n === k) {
return cur.val;
}

cur = cur.right;
}

return -1;
};
29 changes: 29 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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) {
let cur = root;

while (cur) {
if (p.val > cur.val && q.val > cur.val) {
cur = cur.right;
} else if (p.val < cur.val && q.val < cur.val) {
cur = cur.left;
} else {
return cur;
}
}

return null;
};
36 changes: 36 additions & 0 deletions meeting-rooms/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Interval,
} from '/opt/node/lib/lintcode/index.js';

/**
* Definition of Interval:
* class Interval {
* constructor(start, end) {
* this.start = start;
* this.end = end;
* }
* }
*/

export class Solution {
/**
* @param {Interval[]} intervals - an array of meeting time intervals
* @return {boolean} - whether a person could attend all meetings
*/
canAttendMeetings(intervals) {
// start 기준으로 정렬
intervals.sort((a, b) => a.start - b.start);

// 연속된 회의의 시간 비교
for (let i = 1; i < intervals.length; i++) {
const prev = intervals[i - 1];
const curr = intervals[i];

if (prev.end > curr.start) {
return false;
}
}

return true;
}
}