Skip to content

Commit efb1de8

Browse files
authored
Merge pull request #1625 from hsskey/main
[hsskey] WEEK13 Solutions
2 parents 6e78b52 + dccbb14 commit efb1de8

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
class Heap {
2+
constructor(compare) {
3+
this.data = [];
4+
this.compare = compare;
5+
}
6+
7+
size() {
8+
return this.data.length;
9+
}
10+
11+
peek() {
12+
return this.data[0];
13+
}
14+
15+
push(val) {
16+
this.data.push(val);
17+
this._siftUp();
18+
}
19+
20+
pop() {
21+
const top = this.peek();
22+
const bottom = this.data.pop();
23+
if (this.data.length > 0) {
24+
this.data[0] = bottom;
25+
this._siftDown();
26+
}
27+
return top;
28+
}
29+
30+
_siftUp() {
31+
let i = this.data.length - 1;
32+
const node = this.data[i];
33+
while (i > 0) {
34+
const parent = Math.floor((i - 1) / 2);
35+
if (this.compare(node, this.data[parent])) {
36+
this.data[i] = this.data[parent];
37+
i = parent;
38+
} else break;
39+
}
40+
this.data[i] = node;
41+
}
42+
43+
_siftDown() {
44+
let i = 0;
45+
const node = this.data[0];
46+
const length = this.data.length;
47+
48+
while (true) {
49+
let left = 2 * i + 1;
50+
let right = 2 * i + 2;
51+
let swap = i;
52+
53+
if (left < length && this.compare(this.data[left], this.data[swap])) {
54+
swap = left;
55+
}
56+
if (right < length && this.compare(this.data[right], this.data[swap])) {
57+
swap = right;
58+
}
59+
if (swap === i) break;
60+
61+
this.data[i] = this.data[swap];
62+
i = swap;
63+
}
64+
65+
this.data[i] = node;
66+
}
67+
}
68+
69+
var MedianFinder = function() {
70+
this.small = new Heap((a, b) => a > b); // max heap
71+
this.large = new Heap((a, b) => a < b); // min heap
72+
};
73+
74+
MedianFinder.prototype.addNum = function(num) {
75+
this.small.push(num);
76+
77+
if (
78+
this.small.size() > 0 &&
79+
this.large.size() > 0 &&
80+
this.small.peek() > this.large.peek()
81+
) {
82+
this.large.push(this.small.pop());
83+
}
84+
85+
if (this.small.size() > this.large.size() + 1) {
86+
this.large.push(this.small.pop());
87+
}
88+
if (this.large.size() > this.small.size() + 1) {
89+
this.small.push(this.large.pop());
90+
}
91+
};
92+
93+
MedianFinder.prototype.findMedian = function() {
94+
if (this.small.size() > this.large.size()) {
95+
return this.small.peek();
96+
}
97+
if (this.large.size() > this.small.size()) {
98+
return this.large.peek();
99+
}
100+
return (this.small.peek() + this.large.peek()) / 2;
101+
};

โ€Žinsert-interval/hsskey.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @param {number[]} newInterval
4+
* @return {number[][]}
5+
*/
6+
var insert = function(intervals, newInterval) {
7+
const res = [];
8+
9+
for (let i = 0; i < intervals.length; i++) {
10+
if (newInterval[1] < intervals[i][0]) {
11+
res.push(newInterval);
12+
return res.concat(intervals.slice(i));
13+
} else if (newInterval[0] > intervals[i][1]) {
14+
res.push(intervals[i]);
15+
} else {
16+
newInterval = [
17+
Math.min(newInterval[0], intervals[i][0]),
18+
Math.max(newInterval[1], intervals[i][1])
19+
];
20+
}
21+
}
22+
23+
res.push(newInterval);
24+
return res;
25+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val === undefined ? 0 : val);
5+
* this.left = (left === undefined ? null : left);
6+
* this.right = (right === undefined ? null : right);
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @param {number} k
13+
* @return {number}
14+
*/
15+
var kthSmallest = function(root, k) {
16+
let n = 0;
17+
const stack = [];
18+
let cur = root;
19+
20+
while (cur || stack.length > 0) {
21+
while (cur) {
22+
stack.push(cur);
23+
cur = cur.left;
24+
}
25+
26+
cur = stack.pop();
27+
n += 1;
28+
if (n === k) {
29+
return cur.val;
30+
}
31+
32+
cur = cur.right;
33+
}
34+
35+
return -1;
36+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
var lowestCommonAncestor = function(root, p, q) {
16+
let cur = root;
17+
18+
while (cur) {
19+
if (p.val > cur.val && q.val > cur.val) {
20+
cur = cur.right;
21+
} else if (p.val < cur.val && q.val < cur.val) {
22+
cur = cur.left;
23+
} else {
24+
return cur;
25+
}
26+
}
27+
28+
return null;
29+
};

โ€Žmeeting-rooms/hsskey.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
Interval,
3+
} from '/opt/node/lib/lintcode/index.js';
4+
5+
/**
6+
* Definition of Interval:
7+
* class Interval {
8+
* constructor(start, end) {
9+
* this.start = start;
10+
* this.end = end;
11+
* }
12+
* }
13+
*/
14+
15+
export class Solution {
16+
/**
17+
* @param {Interval[]} intervals - an array of meeting time intervals
18+
* @return {boolean} - whether a person could attend all meetings
19+
*/
20+
canAttendMeetings(intervals) {
21+
// start ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
22+
intervals.sort((a, b) => a.start - b.start);
23+
24+
// ์—ฐ์†๋œ ํšŒ์˜์˜ ์‹œ๊ฐ„ ๋น„๊ต
25+
for (let i = 1; i < intervals.length; i++) {
26+
const prev = intervals[i - 1];
27+
const curr = intervals[i];
28+
29+
if (prev.end > curr.start) {
30+
return false;
31+
}
32+
}
33+
34+
return true;
35+
}
36+
}

0 commit comments

Comments
ย (0)