Skip to content

[Jeehay28] WEEK 11 Solutions #1565

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

Merged
merged 4 commits into from
Jun 15, 2025
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
30 changes: 30 additions & 0 deletions binary-tree-maximum-path-sum/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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;
}
}

// TC: O(n)
// SC: O(n)
function maxPathSum(root: TreeNode | null): number {
let maxSum = -Infinity;

const dfs = (node: TreeNode | null) => {
if (!node) return 0;

const leftMax = Math.max(dfs(node.left), 0);
const rightMax = Math.max(dfs(node.right), 0);

maxSum = Math.max(node.val + leftMax + rightMax, maxSum);

return node.val + Math.max(leftMax, rightMax);
};

dfs(root);
return maxSum;
}
22 changes: 22 additions & 0 deletions merge-intervals/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// TC: O(n * log n)
// SC: O(n)
function merge(intervals: number[][]): number[][] {
if (intervals.length === 0) return [];

intervals.sort((a, b) => a[0] - b[0]);

const merged = [intervals[0]];

for (let i = 1; i < intervals.length; i++) {
const last = merged[merged.length - 1];
const current = intervals[i];

if (current[0] <= last[1]) {
last[1] = Math.max(last[1], current[1]);
} else {
merged.push(intervals[i]);
}
}

return merged;
}
30 changes: 30 additions & 0 deletions missing-number/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// TC: O(n)
// SC: O(1)
function missingNumber(nums: number[]): number {
// 0, 1, 2, 3 => n * (n + 1) / 2

let sum = (nums.length * (nums.length + 1)) / 2;

for (const num of nums) {
sum -= num;
}

return sum;
}


// TC: O(n)
// SC: (1)
// function missingNumber(nums: number[]): number {
// let xor = 0;

// for (let i = 0; i <= nums.length; i++) {
// xor ^= i;
// }

// for (const num of nums) {
// xor ^= num;
// }

// return xor;
// }
36 changes: 36 additions & 0 deletions reorder-list/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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;
}
}

/**
Do not return anything, modify head in-place instead.
*/

// TC: O(n)
// SC: O(n)
function reorderList(head: ListNode | null): void {
const nodes: ListNode[] = [];

while (head) {
nodes.push(head);
head = head.next;
}

let start = 0;
let end = nodes.length - 1;

while (start < end) {
nodes[start].next = nodes[end];
start++;
if (start === end) break;
nodes[end].next = nodes[start];
end--;
}

nodes[start].next = null;
}