From a53b3bccbd97b9127ab91afbd9e0474fdcb40acc Mon Sep 17 00:00:00 2001 From: hsskey Date: Sat, 28 Jun 2025 10:41:09 +0900 Subject: [PATCH 1/3] week13 solved --- find-median-from-data-stream/hsskey.js | 103 ++++++++++++++++++ insert-interval/hsskey.js | 27 +++++ kth-smallest-element-in-a-bst/hsskey.js | 38 +++++++ .../hsskey.js | 31 ++++++ meeting-rooms/hsskey.js | 39 +++++++ 5 files changed, 238 insertions(+) create mode 100644 find-median-from-data-stream/hsskey.js create mode 100644 insert-interval/hsskey.js create mode 100644 kth-smallest-element-in-a-bst/hsskey.js create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/hsskey.js create mode 100644 meeting-rooms/hsskey.js diff --git a/find-median-from-data-stream/hsskey.js b/find-median-from-data-stream/hsskey.js new file mode 100644 index 000000000..f80899fd2 --- /dev/null +++ b/find-median-from-data-stream/hsskey.js @@ -0,0 +1,103 @@ +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; + }; + + \ No newline at end of file diff --git a/insert-interval/hsskey.js b/insert-interval/hsskey.js new file mode 100644 index 000000000..4acb89928 --- /dev/null +++ b/insert-interval/hsskey.js @@ -0,0 +1,27 @@ +/** + * @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; + }; + + \ No newline at end of file diff --git a/kth-smallest-element-in-a-bst/hsskey.js b/kth-smallest-element-in-a-bst/hsskey.js new file mode 100644 index 000000000..29865453b --- /dev/null +++ b/kth-smallest-element-in-a-bst/hsskey.js @@ -0,0 +1,38 @@ +/** + * 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; + }; + + \ No newline at end of file diff --git a/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js new file mode 100644 index 000000000..4186ad8ca --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js @@ -0,0 +1,31 @@ +/** + * 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; + }; + + \ No newline at end of file diff --git a/meeting-rooms/hsskey.js b/meeting-rooms/hsskey.js new file mode 100644 index 000000000..c289182d6 --- /dev/null +++ b/meeting-rooms/hsskey.js @@ -0,0 +1,39 @@ +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; + } + } + + + \ No newline at end of file From ce38ea34062816e37b1a681b85879c3dcccc13d6 Mon Sep 17 00:00:00 2001 From: hsskey Date: Sat, 28 Jun 2025 10:44:26 +0900 Subject: [PATCH 2/3] fix: lint err --- find-median-from-data-stream/hsskey.js | 1 - insert-interval/hsskey.js | 1 - kth-smallest-element-in-a-bst/hsskey.js | 1 - lowest-common-ancestor-of-a-binary-search-tree/hsskey.js | 1 - meeting-rooms/hsskey.js | 2 -- 5 files changed, 6 deletions(-) diff --git a/find-median-from-data-stream/hsskey.js b/find-median-from-data-stream/hsskey.js index f80899fd2..6c879f239 100644 --- a/find-median-from-data-stream/hsskey.js +++ b/find-median-from-data-stream/hsskey.js @@ -99,5 +99,4 @@ class Heap { } return (this.small.peek() + this.large.peek()) / 2; }; - \ No newline at end of file diff --git a/insert-interval/hsskey.js b/insert-interval/hsskey.js index 4acb89928..6a543bf4b 100644 --- a/insert-interval/hsskey.js +++ b/insert-interval/hsskey.js @@ -23,5 +23,4 @@ var insert = function(intervals, newInterval) { res.push(newInterval); return res; }; - \ No newline at end of file diff --git a/kth-smallest-element-in-a-bst/hsskey.js b/kth-smallest-element-in-a-bst/hsskey.js index 29865453b..a2f7e8343 100644 --- a/kth-smallest-element-in-a-bst/hsskey.js +++ b/kth-smallest-element-in-a-bst/hsskey.js @@ -34,5 +34,4 @@ var kthSmallest = function(root, k) { return -1; }; - \ No newline at end of file diff --git a/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js index 4186ad8ca..039619a38 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js +++ b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js @@ -27,5 +27,4 @@ var lowestCommonAncestor = function(root, p, q) { return null; }; - \ No newline at end of file diff --git a/meeting-rooms/hsskey.js b/meeting-rooms/hsskey.js index c289182d6..ba3f04661 100644 --- a/meeting-rooms/hsskey.js +++ b/meeting-rooms/hsskey.js @@ -34,6 +34,4 @@ import { return true; } } - - \ No newline at end of file From dccbb1408d93c208761e0152f99de8c8877f911b Mon Sep 17 00:00:00 2001 From: hsskey Date: Sat, 28 Jun 2025 10:47:17 +0900 Subject: [PATCH 3/3] fix: lint err --- find-median-from-data-stream/hsskey.js | 141 +++++++++--------- insert-interval/hsskey.js | 29 ++-- kth-smallest-element-in-a-bst/hsskey.js | 31 ++-- .../hsskey.js | 21 ++- meeting-rooms/hsskey.js | 55 ++++--- 5 files changed, 136 insertions(+), 141 deletions(-) diff --git a/find-median-from-data-stream/hsskey.js b/find-median-from-data-stream/hsskey.js index 6c879f239..a408d1290 100644 --- a/find-median-from-data-stream/hsskey.js +++ b/find-median-from-data-stream/hsskey.js @@ -1,102 +1,101 @@ class Heap { constructor(compare) { - this.data = []; - this.compare = compare; + this.data = []; + this.compare = compare; } - + size() { - return this.data.length; + return this.data.length; } - + peek() { - return this.data[0]; + return this.data[0]; } - + push(val) { - this.data.push(val); - this._siftUp(); + 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; + 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; + 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; + 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; } - if (swap === i) break; - - this.data[i] = this.data[swap]; - i = swap; - } - - this.data[i] = node; + + this.data[i] = node; } - } - - var MedianFinder = function () { +} + +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) { +}; + +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.small.size() > 0 && + this.large.size() > 0 && + this.small.peek() > this.large.peek() ) { - this.large.push(this.small.pop()); + this.large.push(this.small.pop()); } - + if (this.small.size() > this.large.size() + 1) { - this.large.push(this.small.pop()); + this.large.push(this.small.pop()); } if (this.large.size() > this.small.size() + 1) { - this.small.push(this.large.pop()); + this.small.push(this.large.pop()); } - }; - - MedianFinder.prototype.findMedian = function () { +}; + +MedianFinder.prototype.findMedian = function() { if (this.small.size() > this.large.size()) { - return this.small.peek(); + return this.small.peek(); } if (this.large.size() > this.small.size()) { - return this.large.peek(); + return this.large.peek(); } return (this.small.peek() + this.large.peek()) / 2; - }; - \ No newline at end of file +}; diff --git a/insert-interval/hsskey.js b/insert-interval/hsskey.js index 6a543bf4b..6945337d9 100644 --- a/insert-interval/hsskey.js +++ b/insert-interval/hsskey.js @@ -5,22 +5,21 @@ */ 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]) - ]; - } + 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; - }; - \ No newline at end of file +}; diff --git a/kth-smallest-element-in-a-bst/hsskey.js b/kth-smallest-element-in-a-bst/hsskey.js index a2f7e8343..3a6643250 100644 --- a/kth-smallest-element-in-a-bst/hsskey.js +++ b/kth-smallest-element-in-a-bst/hsskey.js @@ -16,22 +16,21 @@ 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; + while (cur) { + stack.push(cur); + cur = cur.left; + } + + cur = stack.pop(); + n += 1; + if (n === k) { + return cur.val; + } + + cur = cur.right; } - + return -1; - }; - \ No newline at end of file +}; diff --git a/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js index 039619a38..8c1a651bd 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js +++ b/lowest-common-ancestor-of-a-binary-search-tree/hsskey.js @@ -14,17 +14,16 @@ */ 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; - } + 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; - }; - \ No newline at end of file +}; diff --git a/meeting-rooms/hsskey.js b/meeting-rooms/hsskey.js index ba3f04661..8789afba9 100644 --- a/meeting-rooms/hsskey.js +++ b/meeting-rooms/hsskey.js @@ -1,37 +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 { +} 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; + // 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; + + return true; } - } - \ No newline at end of file +}