Skip to content

Commit 2d7a9e0

Browse files
authored
Merge pull request #1617 from soobing/week13
[soobing] WEEK13 Solutions
2 parents 6f9ddf0 + 68b49da commit 2d7a9e0

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* 문제 μ„€λͺ…
3+
* - 쀑간 값을 μ°ΎλŠ” 문제
4+
*
5+
* 아이디어 (πŸ‘€ 어렀움..)
6+
* - μ΅œλŒ€ νž™κ³Ό μ΅œμ†Œ νž™μ„ μ‚¬μš©ν•˜μ—¬ 쀑간 값을 μ°ΎλŠ”λ‹€.
7+
*/
8+
class MinHeap {
9+
heap: number[] = [];
10+
size(): number {
11+
return this.heap.length;
12+
}
13+
peek(): number | null {
14+
return this.heap[0] ?? null;
15+
}
16+
push(val: number) {
17+
this.heap.push(val);
18+
this.bubbleUp(this.size() - 1);
19+
}
20+
pop(): number | null {
21+
if (this.size() === 0) return null;
22+
const top = this.heap[0];
23+
const end = this.heap.pop()!;
24+
if (this.size() > 0) {
25+
this.heap[0] = end;
26+
this.bubbleDown(0);
27+
}
28+
return top;
29+
}
30+
private bubbleUp(idx: number) {
31+
while (idx > 0) {
32+
const parent = Math.floor((idx - 1) / 2);
33+
if (this.heap[parent] <= this.heap[idx]) break;
34+
[this.heap[parent], this.heap[idx]] = [this.heap[idx], this.heap[parent]];
35+
idx = parent;
36+
}
37+
}
38+
private bubbleDown(idx: number) {
39+
const n = this.size();
40+
while (true) {
41+
let left = idx * 2 + 1;
42+
let right = idx * 2 + 2;
43+
let smallest = idx;
44+
if (left < n && this.heap[left] < this.heap[smallest]) smallest = left;
45+
if (right < n && this.heap[right] < this.heap[smallest]) smallest = right;
46+
if (smallest === idx) break;
47+
[this.heap[smallest], this.heap[idx]] = [
48+
this.heap[idx],
49+
this.heap[smallest],
50+
];
51+
idx = smallest;
52+
}
53+
}
54+
}
55+
56+
class MaxHeap {
57+
heap: number[] = [];
58+
size(): number {
59+
return this.heap.length;
60+
}
61+
peek(): number | null {
62+
return this.heap[0] ?? null;
63+
}
64+
push(val: number) {
65+
this.heap.push(val);
66+
this.bubbleUp(this.size() - 1);
67+
}
68+
pop(): number | null {
69+
if (this.size() === 0) return null;
70+
const top = this.heap[0];
71+
const end = this.heap.pop()!;
72+
if (this.size() > 0) {
73+
this.heap[0] = end;
74+
this.bubbleDown(0);
75+
}
76+
return top;
77+
}
78+
private bubbleUp(idx: number) {
79+
while (idx > 0) {
80+
const parent = Math.floor((idx - 1) / 2);
81+
if (this.heap[parent] >= this.heap[idx]) break;
82+
[this.heap[parent], this.heap[idx]] = [this.heap[idx], this.heap[parent]];
83+
idx = parent;
84+
}
85+
}
86+
private bubbleDown(idx: number) {
87+
const n = this.size();
88+
while (true) {
89+
let left = idx * 2 + 1;
90+
let right = idx * 2 + 2;
91+
let largest = idx;
92+
if (left < n && this.heap[left] > this.heap[largest]) largest = left;
93+
if (right < n && this.heap[right] > this.heap[largest]) largest = right;
94+
if (largest === idx) break;
95+
[this.heap[largest], this.heap[idx]] = [
96+
this.heap[idx],
97+
this.heap[largest],
98+
];
99+
idx = largest;
100+
}
101+
}
102+
}
103+
104+
class MedianFinder {
105+
private minH = new MinHeap();
106+
private maxH = new MaxHeap();
107+
108+
addNum(num: number): void {
109+
if (this.maxH.size() === 0 || num <= (this.maxH.peek() ?? num)) {
110+
this.maxH.push(num);
111+
} else {
112+
this.minH.push(num);
113+
}
114+
115+
// Rebalance
116+
if (this.maxH.size() > this.minH.size() + 1) {
117+
this.minH.push(this.maxH.pop()!);
118+
} else if (this.minH.size() > this.maxH.size()) {
119+
this.maxH.push(this.minH.pop()!);
120+
}
121+
}
122+
123+
findMedian(): number {
124+
const total = this.maxH.size() + this.minH.size();
125+
if (total % 2 === 1) {
126+
return this.maxH.peek()!;
127+
} else {
128+
return (this.maxH.peek()! + this.minH.peek()!) / 2;
129+
}
130+
}
131+
}

β€Žinsert-interval/soobing.tsβ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 문제 μ„€λͺ…
3+
* - μ£Όμ–΄μ§„ μ‹œκ°„ 간격에 λŒ€ν•΄ 회의λ₯Ό 참석할 수 μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•˜λŠ” 문제
4+
*
5+
* 아이디어
6+
* 1) μ‹œμž‘ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ ν›„, 이전 회의의 μ’…λ£Œ μ‹œκ°„κ³Ό ν˜„μž¬ 회의의 μ‹œμž‘ μ‹œκ°„μ„ λΉ„κ΅ν•˜μ—¬ 참석 κ°€λŠ₯ μ—¬λΆ€λ₯Ό νŒλ‹¨
7+
*
8+
*/
9+
10+
function insert(intervals: number[][], newInterval: number[]): number[][] {
11+
const result: number[][] = [];
12+
let i = 0;
13+
14+
while (i < intervals.length && newInterval[0] > intervals[i][1]) {
15+
result.push(intervals[i]);
16+
i++;
17+
}
18+
19+
while (i < intervals.length && newInterval[1] >= intervals[i][0]) {
20+
newInterval[0] = Math.min(intervals[i][0], newInterval[0]);
21+
newInterval[1] = Math.max(intervals[i][1], newInterval[1]);
22+
i++;
23+
}
24+
result.push(newInterval);
25+
26+
while (i < intervals.length) {
27+
result.push(intervals[i]);
28+
i++;
29+
}
30+
31+
return result;
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 문제 μ„€λͺ…
3+
* - 이진 탐색 트리(BST)κ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, k번째 μž‘μ€ μš”μ†Œλ₯Ό μ°ΎλŠ” 문제
4+
*
5+
* 아이디어
6+
* 1) μ€‘μœ„ 순회λ₯Ό ν•˜λ©΄μ„œ k번째 μž‘μ€ μš”μ†Œλ₯Ό μ°ΎλŠ”λ‹€.
7+
* - 이진 탐색 트리(BST)λŠ” μ€‘μœ„ 순회(in-order traversal)ν•˜λ©΄ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬λœ 값을 얻을 수 μžˆλ‹€.
8+
*/
9+
10+
class TreeNode {
11+
val: number;
12+
left: TreeNode | null;
13+
right: TreeNode | null;
14+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
15+
this.val = val === undefined ? 0 : val;
16+
this.left = left === undefined ? null : left;
17+
this.right = right === undefined ? null : right;
18+
}
19+
}
20+
21+
function kthSmallest(root: TreeNode | null, k: number): number {
22+
let result = -1;
23+
let count = 0;
24+
25+
function inOrder(node: TreeNode | null) {
26+
if (!node || result !== -1) return;
27+
28+
inOrder(node.left);
29+
count++;
30+
31+
if (count === k) {
32+
result = node.val;
33+
return;
34+
}
35+
36+
inOrder(node.right);
37+
}
38+
39+
inOrder(root);
40+
return result;
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 문제 μ„€λͺ…
3+
* - 이진 탐색 트리의 두 λ…Έλ“œμ˜ μ΅œμ†Œ 곡톡 쑰상을 μ°ΎλŠ” 문제
4+
*
5+
* 아이디어
6+
* 1) 두 λ…Έλ“œμ˜ 값을 λΉ„κ΅ν•˜μ—¬ μ΅œμ†Œ 곡톡 쑰상을 μ°ΎλŠ”λ‹€.
7+
* - 두 λ…Έλ“œμ˜ 값이 루트 λ…Έλ“œλ³΄λ‹€ μž‘μœΌλ©΄ μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬λ‘œ 이동
8+
* - 두 λ…Έλ“œμ˜ 값이 루트 λ…Έλ“œλ³΄λ‹€ 크면 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬λ‘œ 이동
9+
* - 두 λ…Έλ“œμ˜ 값이 루트 λ…Έλ“œμ™€ κ°™μœΌλ©΄ 루트 λ…Έλ“œλ₯Ό λ°˜ν™˜
10+
*
11+
*/
12+
13+
function lowestCommonAncestor(
14+
root: TreeNode | null,
15+
p: TreeNode | null,
16+
q: TreeNode | null
17+
): TreeNode | null {
18+
if (root === null) return null;
19+
if (p === null || q === null) return null;
20+
21+
while (root !== null) {
22+
if (p.val < root.val && q.val < root.val) {
23+
root = root.left;
24+
} else if (p.val > root.val && q.val > root.val) {
25+
root = root.right;
26+
} else {
27+
return root;
28+
}
29+
}
30+
31+
return root;
32+
}

β€Žmeeting-rooms/soobing.tsβ€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 문제 μ„€λͺ…
3+
* - μ£Όμ–΄μ§„ μ‹œκ°„ 간격에 λŒ€ν•΄ 회의λ₯Ό 참석할 수 μžˆλŠ”μ§€ μ—¬λΆ€λ₯Ό λ°˜ν™˜ν•˜λŠ” 문제
4+
*
5+
* 아이디어
6+
* 1) μ‹œμž‘ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ ν›„, 이전 회의의 μ’…λ£Œ μ‹œκ°„κ³Ό ν˜„μž¬ 회의의 μ‹œμž‘ μ‹œκ°„μ„ λΉ„κ΅ν•˜μ—¬ 참석 κ°€λŠ₯ μ—¬λΆ€λ₯Ό νŒλ‹¨
7+
*
8+
*/
9+
10+
/**
11+
* Definition of Interval:
12+
* class Interval {
13+
* constructor(start, end) {
14+
* this.start = start;
15+
* this.end = end;
16+
* }
17+
* }
18+
*/
19+
20+
class Solution {
21+
/**
22+
* @param {Interval[]} intervals
23+
* @returns {boolean}
24+
*/
25+
canAttendMeetings(intervals) {
26+
intervals = intervals.sort((a, b) => a.start - b.start);
27+
28+
for (let i = 1; i < intervals.length; i++) {
29+
if (intervals[i].start < intervals[i - 1].end) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}

0 commit comments

Comments
Β (0)