-
-
Notifications
You must be signed in to change notification settings - Fork 195
[mallayon] Week 11 #1039
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
[mallayon] Week 11 #1039
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,30 @@ | ||
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; | ||
} | ||
} | ||
|
||
/** | ||
* @link https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ | ||
* | ||
* 접근 방법 : DFS 사용 | ||
* - 각 노드에서 왼쪽 서브트리와 오른쪽 서브트리 깊이 계산한 후, 더 깊은 값에 1 더하기 | ||
* - 종료 조건 : 노드가 null일 때 0 반환 | ||
* | ||
* 시간복잡도 : O(n) | ||
* - n = 트리의 노드 개수 | ||
* - 노드 한 번씩 방문해서 깊이 계산 | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 기울어진 트리의 경우, 트리 최대 깊이만큼 재귀 호출 스택 쌓임 | ||
*/ | ||
function maxDepth(root: TreeNode | null): number { | ||
if (!root) return 0; | ||
|
||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 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. 공간, 시간 복잡도 계산 및 사용된 변수명들도 잘 정해진것 같습니다 :) |
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 @@ | ||
/** | ||
* @link https://leetcode.com/problems/merge-intervals/ | ||
* | ||
* 접근 방법 : | ||
* - 인터벌 배열 첫 번째 요소 기준으로 오름차순 정렬 | ||
* - 인터벌의 시작이 이전 인터벌 끝보다 작으면 범위 업데이트 | ||
* - 범위에 속하지 않으면 현재 인터벌을 결과 배열에 추가하고 새로운 인터벌로 업데이트 | ||
* | ||
* 시간복잡도 : O(nlogn) | ||
* - n = 인터벌 배열의 길이 | ||
* - 인터벌 배열 정렬 : O(nlogn) | ||
* - 병합 과정: O(n) | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 결과 배열에 담아서 리턴 | ||
*/ | ||
function merge(intervals: number[][]): number[][] { | ||
const result: number[][] = []; | ||
|
||
if (intervals.length < 2) return intervals; | ||
|
||
// 첫번째 요소 기준으로 정렬 | ||
const sortedIntervals = intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
let [start, end] = sortedIntervals[0]; | ||
|
||
for (let i = 1; i < sortedIntervals.length; i++) { | ||
const [newStart, newEnd] = sortedIntervals[i]; | ||
if (newStart <= end) { | ||
// 범위 겹치는 경우, end 업데이트 | ||
end = Math.max(end, newEnd); | ||
} else { | ||
// 겹치지 않는 경우, 현재 구간 추가하고 새로운 구간으로 업데이트 | ||
result.push([start, end]); | ||
[start, end] = [newStart, newEnd]; | ||
} | ||
} | ||
|
||
// 마지막 구간 추가 | ||
result.push([start, end]); | ||
|
||
return result; | ||
} |
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.
크....너무 깔끔하고 좋네요...