-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] WEEK 11 #1036
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
[Jeehay28] WEEK 11 #1036
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa7a304
Add maximum-depth-of-binary-tree solution
Jeehay28 a7e9cad
Add newline at the end of file
Jeehay28 51181ff
Add reorder-list solution
Jeehay28 98df690
Add merge-intervals solution
Jeehay28 7104cf7
Add binary-tree-maximum-path-sum
Jeehay28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// ✅ Time Complexity: O(N) (Each node is visited once) | ||
// ✅ Space Complexity: O(N) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {number} | ||
*/ | ||
var maxPathSum = function (root) { | ||
let maxSum = -Infinity; | ||
|
||
const dfs = (node) => { | ||
if (!node) return 0; | ||
|
||
let leftMax = Math.max(dfs(node.left), 0); | ||
let rightMax = Math.max(dfs(node.right), 0); | ||
|
||
// Compute best path sum that passes through this node | ||
let currentMax = node.val + leftMax + rightMax; | ||
|
||
// Update global maxSum | ||
maxSum = Math.max(maxSum, currentMax); // represents the best path sum for the current node. | ||
|
||
return node.val + Math.max(leftMax, rightMax); // propagates the maximum path sum to the parent node. | ||
}; | ||
|
||
dfs(root); | ||
return maxSum; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Depth-First Search (DFS) with recursion | ||
|
||
// 🕒 Time Complexity: O(n) — where n is the number of nodes in the binary tree. | ||
// 🗂️ Space Complexity: O(h) — where h is the height of the tree. | ||
// ⚙️ How It Works (Example Walkthrough): | ||
// For root = [3,9,20,null,null,15,7]: | ||
// maxDepth(root) = Math.max(maxDepth(9), maxDepth(20)) + 1 = Math.max(1, 2) + 1 = 3 | ||
// maxDepth(9) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1 | ||
// maxDepth(20) = Math.max(maxDepth(15), maxDepth(7)) + 1 = Math.max(1, 1) + 1 = 2 | ||
// maxDepth(15) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1 | ||
// maxDepth(7) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1 | ||
// So the final result: 3 | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = val === undefined ? 0 : val; | ||
* this.next = next === undefined ? null : next; | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode[]} lists | ||
* @return {ListNode} | ||
*/ | ||
|
||
// 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. | ||
var maxDepth = function (root) { | ||
// Base case: if the node is null, the depth is 0 | ||
if (root === null) return 0; | ||
|
||
// Recursively calculate the depth of the left and right subtrees | ||
let leftDepth = maxDepth(root.left); | ||
let rightDepth = maxDepth(root.right); | ||
|
||
// Return the maximum of the two depths plus 1 for the current node | ||
return Math.max(leftDepth, rightDepth) + 1; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 🚀 My own approach! | ||
// ✅ Time Complexity: O(n log n), where n is the number of intervals | ||
// ✅ Space Complexity: O(n) (due to the stack storage) | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @return {number[][]} | ||
*/ | ||
var merge = function (intervals) { | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
// Sorting takes O(n log n) time, where n is the number of intervals. | ||
// JavaScript's sort() is O(1) (in-place) for sorting in most cases. | ||
|
||
let start = intervals[0][0]; | ||
let end = intervals[0][1]; | ||
let stack = [intervals[0]]; // O(n) additional space usage. | ||
|
||
for (const [s, e] of intervals) { | ||
// This takes O(n) time. | ||
const [prevStart, prevEnd] = stack[stack.length - 1]; | ||
|
||
if (prevStart <= s && s <= prevEnd) { | ||
start = Math.min(s, start); | ||
end = Math.max(e, end); | ||
stack.pop(); | ||
} else { | ||
start = s; | ||
end = e; | ||
} | ||
stack.push([start, end]); | ||
} | ||
return stack; | ||
}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stack을 활용해서 문제를 풀이 해 주셨네요! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// ✅ Time Complexity: O(N) | ||
// ✅ Space Complexity: O(N) (due to the stack storage) | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {void} Do not return anything, modify head in-place instead. | ||
*/ | ||
var reorderList = function (head) { | ||
let stack = []; | ||
let node = head; | ||
|
||
while (node) { | ||
stack.push(node); | ||
node = node.next; | ||
} | ||
|
||
let dummy = new ListNode(-1); | ||
node = dummy; | ||
|
||
const len = stack.length; | ||
|
||
for (let i = 0; i < len; i++) { | ||
if (i % 2 === 1) { | ||
node.next = stack.pop(); | ||
} else { | ||
node.next = head; | ||
head = head.next; | ||
} | ||
node = node.next; | ||
} | ||
|
||
node.next = null; | ||
return dummy.next; | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오......자스 문법 활용이 제대로 되서 아주 이쁘네요...!