-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week11 #1029
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
[ganu] Week11 #1029
Changes from 4 commits
e4deb82
7fce08f
88784f2
e6e23e6
ff6d167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Time complexity: O(n) | ||
// 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 answer = Number.MIN_SAFE_INTEGER; | ||
|
||
const dfs = (current) => { | ||
const candidates = [current.val]; | ||
|
||
if (current.left) { | ||
dfs(current.left); | ||
candidates.push(current.left.val + current.val); | ||
} | ||
|
||
if (current.right) { | ||
dfs(current.right); | ||
candidates.push(current.right.val + current.val); | ||
} | ||
|
||
// 현재 노드가 루트일 경우 | ||
if (current.left && current.right) { | ||
answer = Math.max( | ||
answer, | ||
current.left.val + current.right.val + current.val | ||
); | ||
} | ||
|
||
current.val = Math.max(...candidates); | ||
answer = Math.max(answer, current.val); | ||
}; | ||
|
||
dfs(root); | ||
|
||
return answer; | ||
}; |
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. 공간 복잡도는 O(1) 이 사용되지 않을까 싶은데 어떻게 생각하시는지 궁금합니다! 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. 제가 작성한 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Time complexity: O(n) | ||
// 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 maxDepth = function (root) { | ||
let answer = 0; | ||
|
||
const dfs = (current, depth) => { | ||
if (!current) { | ||
return; | ||
} | ||
|
||
if (answer < depth) { | ||
answer = depth; | ||
} | ||
|
||
if (current.left) { | ||
dfs(current.left, depth + 1); | ||
} | ||
|
||
if (current.right) { | ||
dfs(current.right, depth + 1); | ||
} | ||
}; | ||
|
||
dfs(root, 1); | ||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Time complexity: O(nlogn) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @return {number[][]} | ||
*/ | ||
var merge = function (intervals) { | ||
intervals.sort((a, b) => { | ||
if (a[0] === b[0]) { | ||
return a[1] - b[1]; | ||
} | ||
|
||
return a[0] - b[0]; | ||
}); | ||
|
||
const answer = [intervals[0]]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const current = intervals.at(i); | ||
const prev = answer.at(-1); | ||
|
||
// No overlapping | ||
if (current[0] > prev[1]) { | ||
answer.push(current); | ||
continue; | ||
} | ||
|
||
answer[answer.length - 1] = [prev[0], Math.max(prev[1], current[1])]; | ||
} | ||
|
||
return answer; | ||
}; |
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. 해당 문제는 지난번 나왔던 문제중 하나인 Reverse LinkedLIst 를 응용해서 푸는 문제라고 생각해요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* 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) { | ||
const nodes = []; | ||
let n = 0; | ||
|
||
{ | ||
let current = head; | ||
while (current) { | ||
n++; | ||
nodes.push(current); | ||
current = current.next; | ||
} | ||
} | ||
|
||
const answer = head; | ||
|
||
{ | ||
let current = answer; | ||
for (let i = 1; i < n; i++) { | ||
if (i % 2 !== 0) { | ||
current.next = nodes.at(n - Math.ceil(i / 2)); | ||
} else { | ||
current.next = nodes.at(i / 2); | ||
} | ||
|
||
current = current.next; | ||
} | ||
|
||
current.next = null; | ||
} | ||
|
||
return answer; | ||
}; |
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.
공간 복잡도를 O(1) 으로 풀 수 있을것 같아요!