Skip to content

Commit cd7eaed

Browse files
authored
Merge pull request #1031 from mike2ox/main
[moonhyeok] Week 11
2 parents 681733f + d751f6a commit cd7eaed

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

โ€Žgraph-valid-tree/mike2ox.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Source: https://www.lintcode.com/problem/178/
3+
* Solution: ์œ ํšจํ•œ ํŠธ๋ฆฌ์ธ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ํ™•์ธํ•˜๋ฉด ๋˜๊ธฐ์— BFS๋กœ ๊ตฌํ˜„
4+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(V + E) - ๋…ธ๋“œ์™€ ๊ฐ„์„ ์— ํ•œ๋ฒˆ์€ ๋ฐฉ๋ฌธ
5+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(V + E) - ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๋งŒํผ์˜ ๊ณต๊ฐ„ ํ•„์š”
6+
*/
7+
function validTree(n: number, edges: number[][]): boolean {
8+
// ๊ฐ„์„  ๊ฐœ์ˆ˜ ์ฒดํฌ: ํŠธ๋ฆฌ๋Š” ๋…ธ๋“œ ๊ฐœ์ˆ˜ - 1๊ฐœ์˜ ๊ฐ„์„ ์„ ๊ฐ€์ ธ์•ผ ํ•จ
9+
if (edges.length !== n - 1) return false;
10+
11+
// ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ
12+
const adjList: Map<number, number[]> = new Map();
13+
for (let i = 0; i < n; i++) {
14+
adjList.set(i, []);
15+
}
16+
17+
// ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ ๊ตฌ์„ฑ
18+
for (const [u, v] of edges) {
19+
adjList.get(u)!.push(v);
20+
adjList.get(v)!.push(u);
21+
}
22+
23+
const queue: [number, number][] = [[0, -1]]; // [๋…ธ๋“œ, ๋ถ€๋ชจ๋…ธ๋“œ]
24+
const visited: Set<number> = new Set([0]);
25+
26+
while (queue.length > 0) {
27+
const [node, parent] = queue.shift()!;
28+
29+
// ๋ชจ๋“  ์ด์›ƒ ๋…ธ๋“œ ํ™•์ธ
30+
for (const neighbor of adjList.get(node)!) {
31+
// ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” continue
32+
if (neighbor === parent) continue;
33+
34+
// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด ์‚ฌ์ดํด ์กด์žฌ
35+
if (visited.has(neighbor)) return false;
36+
37+
// ์ด์›ƒ ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฉ๋ฌธ ํ‘œ์‹œ
38+
visited.add(neighbor);
39+
queue.push([neighbor, node]);
40+
}
41+
}
42+
43+
// ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
44+
return visited.size === n;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Source: https://leetcode.com/problems/maximum-depth-of-binary-tree/
3+
* ์ ‘๊ทผ๋ฒ•: ์ตœ๋Œ€ ๊นŠ์ด๋งŒ ๊ณ ๋ คํ•˜๋ฉด ๋˜๊ธฐ์— ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜ ์„ ํƒ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(N) - ํŽธํ–ฅ ํŠธ๋ฆฌ์ธ ๊ฒฝ์šฐ, ๋ชจ๋“  ๋…ธ๋“œ(N๊ฐœ) ๊ฒ€์ƒ‰
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(H) - ํŠธ๋ฆฌ๋†’์ด
7+
*
8+
* ๋‹ค๋ฅธ ์ ‘๊ทผ๋ฒ•
9+
* - ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๊ฐ€๋…์„ฑ์žˆ๋Š” ์ฝ”๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ(But, ๊นŠ์ด๊ฐ€ ์ปค์ง€๋ฉด ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* class TreeNode {
15+
* val: number
16+
* left: TreeNode | null
17+
* right: TreeNode | null
18+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
19+
* this.val = (val===undefined ? 0 : val)
20+
* this.left = (left===undefined ? null : left)
21+
* this.right = (right===undefined ? null : right)
22+
* }
23+
* }
24+
*/
25+
26+
// solution1: array๋ฅผ stack์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด์„œ DFS๊ตฌํ˜„
27+
function maxDepth(root: TreeNode | null): number {
28+
if (!root) return 0;
29+
const stack = new Array({ node: root, depth: 1 });
30+
let maxDepth = 1;
31+
while (stack.length) {
32+
const now = stack.pop();
33+
if (!now.node?.left && !now.node?.right) {
34+
if (now.depth > maxDepth) maxDepth = now.depth;
35+
continue;
36+
}
37+
stack.push({ node: now.node.left, depth: now.depth + 1 });
38+
stack.push({ node: now.node.right, depth: now.depth + 1 });
39+
}
40+
return maxDepth;
41+
}
42+
43+
// solution2: recursion์œผ๋กœ DFS๊ตฌํ˜„
44+
function maxDepth(root: TreeNode | null): number {
45+
if (!root) return 0;
46+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
47+
}

โ€Žmerge-intervals/mike2ox.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Source: https://leetcode.com/problems/merge-intervals/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ์ •๋ นํ›„ ์ฒซ๋ฒˆ์งธ ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ดํ›„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฒน์น˜๋Š”์ง€ ํ™•์ธ ํ›„ ๋ณ‘ํ•ฉ or ์ถ”๊ฐ€
4+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(NlogN) - ์ •๋ ฌ์—์„œ NlogN
5+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(N) - ๊ฒฐ๊ณผ ์ €์žฅํ•  ๊ณต๊ฐ„
6+
*/
7+
8+
function merge(intervals: number[][]): number[][] {
9+
if (intervals.length <= 1) return intervals;
10+
11+
intervals.sort((a, b) => a[0] - b[0]);
12+
const result: number[][] = [intervals[0]];
13+
14+
for (let i = 1; i < intervals.length; i++) {
15+
const current = intervals[i];
16+
const lastMerged = result[result.length - 1];
17+
18+
// ํ˜„์žฌ ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์ ์ด ์ด์ „ ๊ตฌ๊ฐ„์˜ ๋์ ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด merge
19+
if (current[0] <= lastMerged[1]) {
20+
lastMerged[1] = Math.max(lastMerged[1], current[1]); // ๋์ ์„ ๋‘ ๊ตฌ๊ฐ„์˜ ๋์  ์ค‘ ๋” ํฐ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ
21+
} else {
22+
result.push(current);
23+
}
24+
}
25+
26+
return result;
27+
}

โ€Žreorder-list/mike2ox.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Source: https://leetcode.com/problems/reorder-list/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ์ž„์‹œ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ํˆฌํฌ์ธํŠธ ์ „๋žต์œผ๋กœ ํ’‚
4+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
*
7+
* ์ถ”๊ฐ€ ํ’€์ด
8+
* - node๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‘ ์ธ์ž๋งŒ ์‚ฌ์šฉํ•ด์„œ ํˆฌํฌ์ธํŠธ ์ „๋žต์ด ๊ฐ€๋Šฅ(but, ๊ตฌํ˜„ x)
9+
/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* class ListNode {
14+
* val: number
15+
* next: ListNode | null
16+
* constructor(val?: number, next?: ListNode | null) {
17+
* this.val = (val===undefined ? 0 : val)
18+
* this.next = (next===undefined ? null : next)
19+
* }
20+
* }
21+
*/
22+
23+
/**
24+
Do not return anything, modify head in-place instead.
25+
*/
26+
function reorderList(head: ListNode | null): void {
27+
if (!head || !head.next) return;
28+
29+
// 1. ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ
30+
const nodes: ListNode[] = [];
31+
let current: ListNode | null = head;
32+
while (current) {
33+
nodes.push(current);
34+
current = current.next;
35+
}
36+
37+
// 2. ๋ฐฐ์—ด์˜ ์–‘๋์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์žฌ๊ตฌ์„ฑ
38+
let left = 0;
39+
let right = nodes.length - 1;
40+
41+
while (left < right) {
42+
// ํ˜„์žฌ ์™ผ์ชฝ ๋…ธ๋“œ์˜ ๋‹ค์Œ์„ ์ €์žฅ
43+
nodes[left].next = nodes[right];
44+
left++;
45+
46+
if (left === right) break;
47+
48+
// ํ˜„์žฌ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ์™ผ์ชฝ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ
49+
nodes[right].next = nodes[left];
50+
right--;
51+
}
52+
53+
// ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next๋ฅผ null๋กœ ์„ค์ •
54+
nodes[left].next = null;
55+
}

0 commit comments

Comments
ย (0)