-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] Week 11 #1028
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
[jdy8739] Week 11 #1028
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8920d82
binary-tree-maximum-path-sum
jdy8739 8ccdaa5
Merge branch 'DaleStudy:main' into main
jdy8739 2e26e59
maximum-depth-of-binary-tree solution
jdy8739 fc83969
maximum-depth-of-binary-tree solution
jdy8739 a828b56
Merge branch 'DaleStudy:main' into main
jdy8739 59e8ed2
reorder-list solution
jdy8739 c67b692
merge-intervals solution
jdy8739 0518c0a
binary-tree-maximum-path-sum fix annot on time complexity
jdy8739 09debd2
merge-intervals refactor by allocationg often referred array to a var…
jdy8739 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,36 @@ | ||
/** | ||
* 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 max = root.val; | ||
|
||
const dfs = (node) => { | ||
if (!node) { | ||
return 0; | ||
} | ||
|
||
const left = Math.max(dfs(node.left), 0); | ||
const right = Math.max(dfs(node.right), 0); | ||
const sum = node.val + left + right; | ||
|
||
max = Math.max(sum, max); | ||
|
||
return node.val + Math.max(left, right); | ||
} | ||
|
||
dfs(root); | ||
|
||
return max; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 트리의 모든 노드를 재귀적으로 탐색하므로 복잡도는 노드의 수와 비례함 | ||
// 공간복잡도 O(h) -> 입력된 트리의 최대 높이만큼 재귀 스택이 쌓이므로 공간복잡도는 트리의 높이와 같음 |
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,32 @@ | ||
/** | ||
* 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 max = 0; | ||
|
||
const dfs = (node, depth) => { | ||
if (node) { | ||
dfs(node.left, depth + 1); | ||
dfs(node.right, depth + 1); | ||
} else { // when this node is null | ||
max = Math.max(max, depth); | ||
} | ||
} | ||
|
||
dfs(root, 0); | ||
|
||
return max; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 트리의 모든 노드를 방문하면서 총 노드의 갯수인 n개 만큼의 시간복잡도를 가지게 되므로 | ||
// 공간복잡도 O(h) -> 콜스택의 최대 길이는 트리의 깊이와 동일하므로 | ||
|
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,29 @@ | ||
/** | ||
* @param {number[][]} intervals | ||
* @return {number[][]} | ||
*/ | ||
var merge = function (intervals) { | ||
const sort = intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
const mergedIntervals = [sort[0]]; | ||
|
||
for (let i = 1; i < sort.length; i++) { | ||
/** 현재 합쳐진 인터벌의 마지막 요소 */ | ||
const lastMergedInterval = mergedIntervals[mergedIntervals.length - 1]; | ||
|
||
const endOfMergedInterval = lastMergedInterval[1]; | ||
|
||
const next = sort[i][0]; | ||
|
||
if (endOfMergedInterval < next) { | ||
mergedIntervals.push(sort[i]); | ||
} else { | ||
lastMergedInterval[1] = Math.max(lastMergedInterval[1], sort[i][1]); | ||
} | ||
} | ||
|
||
return mergedIntervals; | ||
}; | ||
|
||
// 시간복잡도 O(nlogn) -> sort 함수의 시간복잡도가 O(nlogn)이기 때문에 | ||
// 공간복잡도 O(n) -> intervals 배열을 정렬하여 arr이라는 식별자의 배열을 만들어야 하기 때문에 필요한 공간 |
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,54 @@ | ||
/** | ||
* 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) { | ||
if (!head) { | ||
return null; | ||
} | ||
|
||
const stack = []; | ||
|
||
let node = head; | ||
|
||
while (node) { | ||
stack.push(node); | ||
node = node.next; | ||
} | ||
|
||
const length = stack.length; | ||
|
||
node = head; | ||
let count = 0; | ||
|
||
while (count < length) { | ||
if (count % 2 === 0) { | ||
const top = stack.pop(); | ||
|
||
top.next = node.next; | ||
|
||
node.next = top; | ||
} | ||
|
||
if (count === length - 1) { | ||
node.next = null; | ||
} else { | ||
node = node.next; | ||
} | ||
|
||
count++; | ||
} | ||
|
||
|
||
return head; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> while문이 링크드리스트의 길이만큼 순회를하기때문에 링크드리스트의 길이만큼 시간이 걸림 | ||
// 공간복잡도 O(n) -> 스택에 모든 노드를 저장하기 때문에 링크드리스트의 길이만큼 공간이 필요 | ||
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.
지금 풀이도 충분히 좋습니다!
하지만 아무래도 링크드 리스트에 대한 조작 문제이고, 아직 여유 시간도 좀 더 있는 만큼
runner
를 사용한 풀이도 한번 시도해보시면 어떨까요?공간 복잡도를 O(1)으로 줄일 수도 있으니까요!