Skip to content
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 5 commits into from
Oct 27, 2024
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
22 changes: 22 additions & 0 deletions maximum-depth-of-binary-tree/gitsunmin.ts
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;
};
53 changes: 53 additions & 0 deletions reorder-list/gitsunmin.ts
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;
}
Comment on lines +19 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전에 cycle 찾을 때 봤던 알고리즘인데, 이러게 중간 지점 찾는 용도로도 사용할 수 있군요


// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 전체 리스트를 뒤집어 놓고, 원래 리스트랑 뒤집은 리스트랑 같이 조회하면서 병합시켰는데, 이렇게하면 메모리를 아낄 수 있는 좋은 풀이인 것 같습니다.

};