-
Notifications
You must be signed in to change notification settings - Fork 126
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
[gitsunmin] WEEK11 Solutions #555
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0dbcb2
Add week 11 solutions: maximum-depth-of-binary-tree
gitsunmin 353b541
Add week 11 solutions: reorder-list
gitsunmin 05ecc25
Merge branch 'DaleStudy:main' into main
gitsunmin c6d6330
add line break
gitsunmin 73b2fc1
Merge branch 'DaleStudy:main' into main
gitsunmin 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 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,22 @@ | ||
/** | ||
* https://leetcode.com/problems/maximum-depth-of-binary-tree/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
|
||
export 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) | ||
} | ||
} | ||
|
||
export function maxDepth(root: TreeNode | null): number { | ||
if (!root) return 0; | ||
|
||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; | ||
}; |
This file contains 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,53 @@ | ||
/** | ||
* https://leetcode.com/problems/reorder-list/ | ||
* time complexity : O(n) | ||
* space complexity : O(1) | ||
*/ | ||
|
||
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) | ||
} | ||
} | ||
|
||
function reorderList(head: ListNode | null): void { | ||
if (!head || !head.next) return; | ||
|
||
// 1. 중간 지점 찾기 (Floyd’s Tortoise and Hare) | ||
let slow: ListNode | null = head; | ||
let fast: ListNode | null = head; | ||
|
||
while (fast && fast.next) { | ||
slow = slow!.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
// 2. 중간 이후의 리스트 뒤집기 | ||
let prev: ListNode | null = null; | ||
let curr: ListNode | null = slow; | ||
|
||
while (curr) { | ||
const next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
|
||
// 3. 앞부분과 뒤집어진 후반부 병합 | ||
let first: ListNode | null = head; | ||
let second: ListNode | null = prev; | ||
|
||
while (second && second.next) { | ||
const temp1 = first!.next; | ||
const temp2 = second.next; | ||
|
||
first!.next = second; | ||
second.next = temp1; | ||
|
||
first = temp1; | ||
second = temp2; | ||
} | ||
Comment on lines
+40
to
+52
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. 저는 전체 리스트를 뒤집어 놓고, 원래 리스트랑 뒤집은 리스트랑 같이 조회하면서 병합시켰는데, 이렇게하면 메모리를 아낄 수 있는 좋은 풀이인 것 같습니다. |
||
}; |
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.
전에 cycle 찾을 때 봤던 알고리즘인데, 이러게 중간 지점 찾는 용도로도 사용할 수 있군요