Skip to content
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

[강희찬] WEEK 13 Solution #580

Merged
merged 5 commits into from
Nov 10, 2024
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
164 changes: 164 additions & 0 deletions find-median-from-data-stream/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* https://leetcode.com/problems/find-median-from-data-stream
* array with binary search
*/
class MedianFinder {
// S.C. O(n)
private nums: number[] = [];

// T.C. O(n)
addNum(num: number): void {
if (this.nums.length === 0) {
this.nums.push(num);
return;
} else {
this.putNumWithBinarySearch(num);
}
}

private putNumWithBinarySearch(num: number): void {
let left = 0;
let right = this.nums.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (this.nums[mid] === num) {
this.nums.splice(mid, 0, num);
return;
} else if (this.nums[mid] < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}

// T.C. O(n)
this.nums.splice(left, 0, num);
}

// T.C. O(1)
findMedian(): number {
const len = this.nums.length;

if (len % 2 === 1) {
return this.nums[Math.floor(len / 2)];
} else {
return (this.nums[len / 2] + this.nums[len / 2 - 1]) / 2;
}
}
}

/**
* heap...
* TL;DR
*/
class MedianFinder {
// S.C. O(n)
private smallHalf: MaxHeap = new MaxHeap();
private largeHalf: MinHeap = new MinHeap();

// T.C. O(log n)
addNum(num: number): void {
this.smallHalf.push(num);
this.largeHalf.push(this.smallHalf.pop()!);

if (this.smallHalf.size() < this.largeHalf.size()) {
this.smallHalf.push(this.largeHalf.pop()!);
}
}

// T.C. O(1)
findMedian(): number {
if (this.smallHalf.size() === this.largeHalf.size()) {
return (this.smallHalf.peek()! + this.largeHalf.peek()!) / 2;
} else {
return this.smallHalf.peek()!;
}
}
}

class MinHeap {
protected heap: number[] = [];

push(val: number): void {
this.heap.push(val);
this.heapifyUp();
}

pop(): number | undefined {
if (this.heap.length === 0) return undefined;
if (this.heap.length === 1) return this.heap.pop();

const result = this.heap[0];
this.heap[0] = this.heap.pop()!;
this.heapifyDown();
return result;
}

peek(): number | undefined {
return this.heap[0];
}

size(): number {
return this.heap.length;
}

private heapifyUp(): void {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[parentIndex] <= this.heap[index]) break;

this.swap(parentIndex, index);
index = parentIndex;
}
}

private heapifyDown(): void {
let index = 0;
while (true) {
let smallest = index;
const leftChild = 2 * index + 1;
const rightChild = 2 * index + 2;

if (
leftChild < this.heap.length &&
this.heap[leftChild] < this.heap[smallest]
) {
smallest = leftChild;
}

if (
rightChild < this.heap.length &&
this.heap[rightChild] < this.heap[smallest]
) {
smallest = rightChild;
}

if (smallest === index) break;

this.swap(index, smallest);
index = smallest;
}
}

protected swap(i: number, j: number): void {
[this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
}
}

class MaxHeap extends MinHeap {
push(val: number): void {
super.push(-val);
}

pop(): number | undefined {
const val = super.pop();
return val === undefined ? undefined : -val;
}

peek(): number | undefined {
const val = super.peek();
return val === undefined ? undefined : -val;
}
}
15 changes: 15 additions & 0 deletions house-robber/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* https://leetcode.com/problems/house-robber
* T.C. O(n)
* S.C. O(1)
*/
function rob(nums: number[]): number {
let prev = 0;
let curr = 0;
for (let i = 0; i < nums.length; i++) {
const temp = curr;
curr = Math.max(prev + nums[i], curr);
prev = temp;
}
return curr;
}
52 changes: 52 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}

/**
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description
* T.C. O(log n)
* S.C. O(log n)
*/
function lowestCommonAncestor(
root: TreeNode | null,
p: TreeNode | null,
q: TreeNode | null
): TreeNode | null {
if (!root) return null;
if (root.val > p!.val && root.val > q!.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (root.val < p!.val && root.val < q!.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}

/**
* iterative
* T.C. O(log n)
* S.C. O(1)
*/
function lowestCommonAncestor(
root: TreeNode | null,
p: TreeNode | null,
q: TreeNode | null
): TreeNode | null {
while (root) {
if (root.val > p!.val && root.val > q!.val) {
root = root.left;
} else if (root.val < p!.val && root.val < q!.val) {
root = root.right;
} else {
return root;
}
}
return null;
}
12 changes: 12 additions & 0 deletions meeting-rooms/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* https://leetcode.com/problems/meeting-rooms
* T.C. O(nlogn)
* S.C. O(1)
*/
function canAttendMeetings(intervals: number[][]): boolean {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < intervals.length - 1; i++) {
if (intervals[i][1] > intervals[i + 1][0]) return false;
}
return true;
}
29 changes: 29 additions & 0 deletions non-overlapping-intervals/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* https://leetcode.com/problems/non-overlapping-intervals
* T.C. O(n logn)
* S.C. O(1)
*
* [[1,2],[2,3],[3,4],[1,3]] =(sort by end)=> [[1,2],[2,3],[1,3],[3,4]]
*
* 0 1 2 3 4...
* [=) pass
* [=) pass
* [===) count++
* ^^^
* [=) pass
*/
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);

let count = 0;
let end = -Infinity;

for (let i = 0; i < intervals.length; i++) {
if (intervals[i][0] < end) { //
count++;
} else {
end = intervals[i][1];
}
}
return count;
}