Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions find-minimum-in-rotated-sorted-array/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Approach 2: binary search
// Time Complexity: ✅ O(log n)
// Space Complexity: O(1)

function findMin(nums: number[]): number {

let left = 0
let right = nums.length - 1

while(left < right) {

const mid = Math.floor((left + right) / 2)

if(nums[mid] > nums[right]) {
// the min must be to the right of mid
left = mid + 1
} else {
// the mins could be mid or to the left
right = mid
}
}

return nums[left]
};


// Approach 1:
// Time Complexity: ❌ O(n)
// Space Complexity: O(1)

// function findMin(nums: number[]): number {
// // input: an array of length n sorted in ascending order
// // rotate: a[n-1], a[0], ..., a[n-2]
// // time complexity allowed: O(log n)

// let first = nums[0];
// let last = nums[nums.length - 1];
// let cnt = 0;

// while (first > last) {
// first = last;
// cnt += 1;
// last = nums[nums.length - 1 - cnt];
// }

// return first;
// }

75 changes: 75 additions & 0 deletions maximum-depth-of-binary-tree/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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;
}
}

// Approach 3:
// Time Complexity: O(n)
// Space Complexity: O(n), due to the recursion stack

function maxDepth(root: TreeNode | null): number {
if (!root) return 0;

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}

// Approach 2:
// Time Complexity: O(n)
// Space Complexity: O(n)

// function maxDepth(root: TreeNode | null): number {
// if (!root) return 0;

// let maxDepth = 0;
// let stack: Array<[TreeNode, number]> = [[root, 1]];

// while (stack.length > 0) {
// const item = stack.pop();

// if (!item) continue;

// const [node, depth] = item;

// maxDepth = Math.max(maxDepth, depth);

// if (node.left) {
// stack.push([node.left, depth + 1]);
// }

// if (node.right) {
// stack.push([node.right, depth + 1]);
// }
// }

// return maxDepth;
// }


// Approach 1
// Time Compleixty:O(n)
// Space Complexity:O(n), due to the recursion stack

// function maxDepth(root: TreeNode | null): number {
// let maxCnt = 0;

// const dfs = (node: TreeNode | null, cnt: number) => {
// if (!node) {
// maxCnt = Math.max(maxCnt, cnt);
// return;
// }

// dfs(node.left, cnt + 1);
// dfs(node.right, cnt + 1);
// };

// dfs(root, 0);

// return maxCnt;
// }

82 changes: 82 additions & 0 deletions merge-two-sorted-lists/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Approach 2:
// ✅ Time Complexity: O(n)
// ✅ Space Complexity: O(1)

class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}

function mergeTwoLists(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
let dummy = new ListNode(0);
let current = dummy;

while (list1 && list2) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}

current.next = list1 || list2; // Attach whatever is left

return dummy.next;
}


// Approach 1: works, but not efficient for big inputs
// Time Complexity: O(n log n)
// Space Complexity: O(n)

// function mergeTwoLists(
// list1: ListNode | null,
// list2: ListNode | null
// ): ListNode | null {
// let stack: number[] = [];

// const dfs = (node: ListNode | null) => {
// if (!node) {
// return;
// }

// stack.push(node.val);

// return dfs(node.next);
// };

// dfs(list1);
// dfs(list2);

// stack.sort((a, b) => a - b);

// if (stack.length === 0) {
// return null;
// }

// let merged = new ListNode();
// let dummy = merged;

// for (let i = 0; i < stack.length; i++) {
// dummy.val = stack[i];

// if (i !== stack.length - 1) {
// dummy.next = new ListNode();
// dummy = dummy.next;
// } else {
// dummy.next = null;
// }
// }

// return merged;
// }