Skip to content

[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 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions maximum-depth-of-binary-tree/mmyeon.ts
Copy link
Contributor

Choose a reason for hiding this comment

The 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,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;
}
43 changes: 43 additions & 0 deletions merge-intervals/mmyeon.ts
Copy link
Contributor

Choose a reason for hiding this comment

The 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,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;
}