Skip to content

Commit fd1ca34

Browse files
authored
Merge pull request #1036 from Jeehay28/main
[Jeehay28] WEEK 11
2 parents 4210a48 + 7104cf7 commit fd1ca34

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ✅ Time Complexity: O(N) (Each node is visited once)
2+
// ✅ Space Complexity: O(N)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {number}
15+
*/
16+
var maxPathSum = function (root) {
17+
let maxSum = -Infinity;
18+
19+
const dfs = (node) => {
20+
if (!node) return 0;
21+
22+
let leftMax = Math.max(dfs(node.left), 0);
23+
let rightMax = Math.max(dfs(node.right), 0);
24+
25+
// Compute best path sum that passes through this node
26+
let currentMax = node.val + leftMax + rightMax;
27+
28+
// Update global maxSum
29+
maxSum = Math.max(maxSum, currentMax); // represents the best path sum for the current node.
30+
31+
return node.val + Math.max(leftMax, rightMax); // propagates the maximum path sum to the parent node.
32+
};
33+
34+
dfs(root);
35+
return maxSum;
36+
};
37+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Depth-First Search (DFS) with recursion
2+
3+
// 🕒 Time Complexity: O(n) — where n is the number of nodes in the binary tree.
4+
// 🗂️ Space Complexity: O(h) — where h is the height of the tree.
5+
// ⚙️ How It Works (Example Walkthrough):
6+
// For root = [3,9,20,null,null,15,7]:
7+
// maxDepth(root) = Math.max(maxDepth(9), maxDepth(20)) + 1 = Math.max(1, 2) + 1 = 3
8+
// maxDepth(9) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
9+
// maxDepth(20) = Math.max(maxDepth(15), maxDepth(7)) + 1 = Math.max(1, 1) + 1 = 2
10+
// maxDepth(15) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
11+
// maxDepth(7) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
12+
// So the final result: 3
13+
14+
/**
15+
* Definition for singly-linked list.
16+
* function ListNode(val, next) {
17+
* this.val = val === undefined ? 0 : val;
18+
* this.next = next === undefined ? null : next;
19+
* }
20+
*/
21+
/**
22+
* @param {ListNode[]} lists
23+
* @return {ListNode}
24+
*/
25+
26+
// For maximum depth, the presence of one child doesn't matter much because we are looking for the deepest path from the root to any leaf.
27+
var maxDepth = function (root) {
28+
// Base case: if the node is null, the depth is 0
29+
if (root === null) return 0;
30+
31+
// Recursively calculate the depth of the left and right subtrees
32+
let leftDepth = maxDepth(root.left);
33+
let rightDepth = maxDepth(root.right);
34+
35+
// Return the maximum of the two depths plus 1 for the current node
36+
return Math.max(leftDepth, rightDepth) + 1;
37+
};
38+

merge-intervals/Jeehay28.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 🚀 My own approach!
2+
// ✅ Time Complexity: O(n log n), where n is the number of intervals
3+
// ✅ Space Complexity: O(n) (due to the stack storage)
4+
5+
/**
6+
* @param {number[][]} intervals
7+
* @return {number[][]}
8+
*/
9+
var merge = function (intervals) {
10+
intervals.sort((a, b) => a[0] - b[0]);
11+
// Sorting takes O(n log n) time, where n is the number of intervals.
12+
// JavaScript's sort() is O(1) (in-place) for sorting in most cases.
13+
14+
let start = intervals[0][0];
15+
let end = intervals[0][1];
16+
let stack = [intervals[0]]; // O(n) additional space usage.
17+
18+
for (const [s, e] of intervals) {
19+
// This takes O(n) time.
20+
const [prevStart, prevEnd] = stack[stack.length - 1];
21+
22+
if (prevStart <= s && s <= prevEnd) {
23+
start = Math.min(s, start);
24+
end = Math.max(e, end);
25+
stack.pop();
26+
} else {
27+
start = s;
28+
end = e;
29+
}
30+
stack.push([start, end]);
31+
}
32+
return stack;
33+
};
34+

reorder-list/Jeehay28.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ✅ Time Complexity: O(N)
2+
// ✅ Space Complexity: O(N) (due to the stack storage)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* function ListNode(val, next) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
*/
11+
12+
/**
13+
* @param {ListNode} head
14+
* @return {void} Do not return anything, modify head in-place instead.
15+
*/
16+
var reorderList = function (head) {
17+
let stack = [];
18+
let node = head;
19+
20+
while (node) {
21+
stack.push(node);
22+
node = node.next;
23+
}
24+
25+
let dummy = new ListNode(-1);
26+
node = dummy;
27+
28+
const len = stack.length;
29+
30+
for (let i = 0; i < len; i++) {
31+
if (i % 2 === 1) {
32+
node.next = stack.pop();
33+
} else {
34+
node.next = head;
35+
head = head.next;
36+
}
37+
node = node.next;
38+
}
39+
40+
node.next = null;
41+
return dummy.next;
42+
};
43+

0 commit comments

Comments
 (0)