Skip to content

Commit 89b1053

Browse files
authored
Merge pull request #580 from HC-kang/main
[강희찬] WEEK 13 Solution
2 parents e4ac6b1 + a1adfa1 commit 89b1053

File tree

5 files changed

+272
-0
lines changed

5 files changed

+272
-0
lines changed
+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* https://leetcode.com/problems/find-median-from-data-stream
3+
* array with binary search
4+
*/
5+
class MedianFinder {
6+
// S.C. O(n)
7+
private nums: number[] = [];
8+
9+
// T.C. O(n)
10+
addNum(num: number): void {
11+
if (this.nums.length === 0) {
12+
this.nums.push(num);
13+
return;
14+
} else {
15+
this.putNumWithBinarySearch(num);
16+
}
17+
}
18+
19+
private putNumWithBinarySearch(num: number): void {
20+
let left = 0;
21+
let right = this.nums.length - 1;
22+
23+
while (left <= right) {
24+
const mid = Math.floor((left + right) / 2);
25+
if (this.nums[mid] === num) {
26+
this.nums.splice(mid, 0, num);
27+
return;
28+
} else if (this.nums[mid] < num) {
29+
left = mid + 1;
30+
} else {
31+
right = mid - 1;
32+
}
33+
}
34+
35+
// T.C. O(n)
36+
this.nums.splice(left, 0, num);
37+
}
38+
39+
// T.C. O(1)
40+
findMedian(): number {
41+
const len = this.nums.length;
42+
43+
if (len % 2 === 1) {
44+
return this.nums[Math.floor(len / 2)];
45+
} else {
46+
return (this.nums[len / 2] + this.nums[len / 2 - 1]) / 2;
47+
}
48+
}
49+
}
50+
51+
/**
52+
* heap...
53+
* TL;DR
54+
*/
55+
class MedianFinder {
56+
// S.C. O(n)
57+
private smallHalf: MaxHeap = new MaxHeap();
58+
private largeHalf: MinHeap = new MinHeap();
59+
60+
// T.C. O(log n)
61+
addNum(num: number): void {
62+
this.smallHalf.push(num);
63+
this.largeHalf.push(this.smallHalf.pop()!);
64+
65+
if (this.smallHalf.size() < this.largeHalf.size()) {
66+
this.smallHalf.push(this.largeHalf.pop()!);
67+
}
68+
}
69+
70+
// T.C. O(1)
71+
findMedian(): number {
72+
if (this.smallHalf.size() === this.largeHalf.size()) {
73+
return (this.smallHalf.peek()! + this.largeHalf.peek()!) / 2;
74+
} else {
75+
return this.smallHalf.peek()!;
76+
}
77+
}
78+
}
79+
80+
class MinHeap {
81+
protected heap: number[] = [];
82+
83+
push(val: number): void {
84+
this.heap.push(val);
85+
this.heapifyUp();
86+
}
87+
88+
pop(): number | undefined {
89+
if (this.heap.length === 0) return undefined;
90+
if (this.heap.length === 1) return this.heap.pop();
91+
92+
const result = this.heap[0];
93+
this.heap[0] = this.heap.pop()!;
94+
this.heapifyDown();
95+
return result;
96+
}
97+
98+
peek(): number | undefined {
99+
return this.heap[0];
100+
}
101+
102+
size(): number {
103+
return this.heap.length;
104+
}
105+
106+
private heapifyUp(): void {
107+
let index = this.heap.length - 1;
108+
while (index > 0) {
109+
const parentIndex = Math.floor((index - 1) / 2);
110+
if (this.heap[parentIndex] <= this.heap[index]) break;
111+
112+
this.swap(parentIndex, index);
113+
index = parentIndex;
114+
}
115+
}
116+
117+
private heapifyDown(): void {
118+
let index = 0;
119+
while (true) {
120+
let smallest = index;
121+
const leftChild = 2 * index + 1;
122+
const rightChild = 2 * index + 2;
123+
124+
if (
125+
leftChild < this.heap.length &&
126+
this.heap[leftChild] < this.heap[smallest]
127+
) {
128+
smallest = leftChild;
129+
}
130+
131+
if (
132+
rightChild < this.heap.length &&
133+
this.heap[rightChild] < this.heap[smallest]
134+
) {
135+
smallest = rightChild;
136+
}
137+
138+
if (smallest === index) break;
139+
140+
this.swap(index, smallest);
141+
index = smallest;
142+
}
143+
}
144+
145+
protected swap(i: number, j: number): void {
146+
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
147+
}
148+
}
149+
150+
class MaxHeap extends MinHeap {
151+
push(val: number): void {
152+
super.push(-val);
153+
}
154+
155+
pop(): number | undefined {
156+
const val = super.pop();
157+
return val === undefined ? undefined : -val;
158+
}
159+
160+
peek(): number | undefined {
161+
const val = super.peek();
162+
return val === undefined ? undefined : -val;
163+
}
164+
}

house-robber/HC-kang.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* https://leetcode.com/problems/house-robber
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
*/
6+
function rob(nums: number[]): number {
7+
let prev = 0;
8+
let curr = 0;
9+
for (let i = 0; i < nums.length; i++) {
10+
const temp = curr;
11+
curr = Math.max(prev + nums[i], curr);
12+
prev = temp;
13+
}
14+
return curr;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description
14+
* T.C. O(log n)
15+
* S.C. O(log n)
16+
*/
17+
function lowestCommonAncestor(
18+
root: TreeNode | null,
19+
p: TreeNode | null,
20+
q: TreeNode | null
21+
): TreeNode | null {
22+
if (!root) return null;
23+
if (root.val > p!.val && root.val > q!.val) {
24+
return lowestCommonAncestor(root.left, p, q);
25+
} else if (root.val < p!.val && root.val < q!.val) {
26+
return lowestCommonAncestor(root.right, p, q);
27+
} else {
28+
return root;
29+
}
30+
}
31+
32+
/**
33+
* iterative
34+
* T.C. O(log n)
35+
* S.C. O(1)
36+
*/
37+
function lowestCommonAncestor(
38+
root: TreeNode | null,
39+
p: TreeNode | null,
40+
q: TreeNode | null
41+
): TreeNode | null {
42+
while (root) {
43+
if (root.val > p!.val && root.val > q!.val) {
44+
root = root.left;
45+
} else if (root.val < p!.val && root.val < q!.val) {
46+
root = root.right;
47+
} else {
48+
return root;
49+
}
50+
}
51+
return null;
52+
}

meeting-rooms/HC-kang.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* https://leetcode.com/problems/meeting-rooms
3+
* T.C. O(nlogn)
4+
* S.C. O(1)
5+
*/
6+
function canAttendMeetings(intervals: number[][]): boolean {
7+
intervals.sort((a, b) => a[0] - b[0]);
8+
for (let i = 0; i < intervals.length - 1; i++) {
9+
if (intervals[i][1] > intervals[i + 1][0]) return false;
10+
}
11+
return true;
12+
}

non-overlapping-intervals/HC-kang.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* https://leetcode.com/problems/non-overlapping-intervals
3+
* T.C. O(n logn)
4+
* S.C. O(1)
5+
*
6+
* [[1,2],[2,3],[3,4],[1,3]] =(sort by end)=> [[1,2],[2,3],[1,3],[3,4]]
7+
*
8+
* 0 1 2 3 4...
9+
* [=) pass
10+
* [=) pass
11+
* [===) count++
12+
* ^^^
13+
* [=) pass
14+
*/
15+
function eraseOverlapIntervals(intervals: number[][]): number {
16+
intervals.sort((a, b) => a[1] - b[1]);
17+
18+
let count = 0;
19+
let end = -Infinity;
20+
21+
for (let i = 0; i < intervals.length; i++) {
22+
if (intervals[i][0] < end) { //
23+
count++;
24+
} else {
25+
end = intervals[i][1];
26+
}
27+
}
28+
return count;
29+
}

0 commit comments

Comments
 (0)