Skip to content

Commit fc5692c

Browse files
authored
Merge pull request #1553 from soobing/week10
[soobing] WEEK 10 Solutions
2 parents 6de04e8 + d2800d8 commit fc5692c

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

โ€Žcourse-schedule/soobing.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* ๋ฌธ์ œ ํ•ด์„ค
3+
* - ๊ทธ๋ž˜ํ”„์— ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* 1) ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ -> DFS, BFS
7+
* - ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
8+
* - ํ˜„์žฌ ๊ฒฝ๋กœ์— ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ๋งŒ๋‚˜๋ฉด ์‚ฌ์ดํด ๋ฐœ์ƒ. = 1
9+
*/
10+
11+
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
12+
const graph = new Map<number, number[]>();
13+
const visited: number[] = new Array(numCourses).fill(0); // 0: ์•ˆ ๋‹ค๋…€์˜ด, 1: ์‚ฌ์ดํด ํ™•์ธ ์ฆ(๋ฐฉ๋ฌธ ์ค‘), 2: ์‚ฌ์ดํด ์—†์Œ ํ™•์ธ ์™„๋ฃŒ
14+
15+
// ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
16+
for (const [course, preCourse] of prerequisites) {
17+
if (!graph.has(course)) graph.set(course, []);
18+
graph.get(course)!.push(preCourse);
19+
}
20+
21+
function hasCycle(index: number) {
22+
if (visited[index] === 1) return true;
23+
if (visited[index] === 2) return false;
24+
25+
visited[index] = 1;
26+
for (const preCourse of graph.get(index) || []) {
27+
if (hasCycle(preCourse)) return true;
28+
}
29+
visited[index] = 2;
30+
return false;
31+
}
32+
33+
for (let i = 0; i < numCourses; i++) {
34+
if (hasCycle(i)) return false;
35+
}
36+
return true;
37+
}

โ€Žinvert-binary-tree/soobing.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๋ฐ˜์ „์‹œํ‚ค๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* 1) DFS / BFS ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋ฐ˜์ „์‹œํ‚ค๊ธฐ
7+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n): ๋ชจ๋“  ๋…ธ๋“œ ํ•œ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ
8+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„ DFS์˜ ๊ฒฝ์šฐ O(h), BFS์˜ ๊ฒฝ์šฐ O(2/n) -> ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ ์ˆ˜
9+
*/
10+
11+
class TreeNode {
12+
val: number;
13+
left: TreeNode | null;
14+
right: TreeNode | null;
15+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
16+
this.val = val === undefined ? 0 : val;
17+
this.left = left === undefined ? null : left;
18+
this.right = right === undefined ? null : right;
19+
}
20+
}
21+
22+
function invertTree(root: TreeNode | null): TreeNode | null {
23+
if (!root) return null;
24+
25+
const left = invertTree(root.left);
26+
const right = invertTree(root.right);
27+
28+
root.left = right;
29+
root.right = left;
30+
31+
return root;
32+
}
33+
34+
/**
35+
* Definition for a binary tree node.
36+
* class TreeNode {
37+
* val: number
38+
* left: TreeNode | null
39+
* right: TreeNode | null
40+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
41+
* this.val = (val===undefined ? 0 : val)
42+
* this.left = (left===undefined ? null : left)
43+
* this.right = (right===undefined ? null : right)
44+
* }
45+
* }
46+
*/
47+
48+
function invertTreeBFS(root: TreeNode | null): TreeNode | null {
49+
const queue: (TreeNode | null)[] = [root];
50+
51+
while (queue.length > 0) {
52+
const current = queue.shift();
53+
if (current) {
54+
const left = current.left;
55+
const right = current.right;
56+
current.left = right;
57+
current.right = left;
58+
59+
queue.push(left);
60+
queue.push(right);
61+
}
62+
}
63+
return root;
64+
}

โ€Žjump-game/soobing.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* ๋ฌธ์ œ ํ•ด์„ค
3+
* - ํ˜„์žฌ ์œ„์น˜์—์„œ ์ตœ๋Œ€ ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐฏ์ˆ˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด, ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์— ๋„๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* 1) ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
7+
* - ๋ฐฐ์—ด์„ ์ญ‰ ์ˆœํšŒํ•˜๋ฉด์„œ ๋‹ค์Œ ์ด๋™ ๊ฐ€๋Šฅํ•œ ํšŸ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹ค์Œ ํ•ญ๋ชฉ์œผ๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•œ์ง€ ์ฒดํฌ, ์—†๋‹ค๋ฉด false ๋ฐ˜ํ™˜.
8+
* - ์ผ๋‹จ ํ˜„์žฌ๊นŒ์ง€ ์™”๋‹ค๋ฉด ์ดํ›„์— ์ตœ๋Œ€๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ์—…๋ฐ์ดํŠธ.
9+
*/
10+
function canJump(nums: number[]): boolean {
11+
let reachable = 0;
12+
for (let i = 0; i < nums.length; i++) {
13+
if (i > reachable) return false;
14+
reachable = Math.max(reachable, i + nums[i]);
15+
}
16+
return true;
17+
}

โ€Žmerge-k-sorted-lists/soobing.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - k๊ฐœ์˜ ์ •๋ ฌ๋œ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋ณ‘ํ•ฉํ•˜๊ธฐ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* 1) ๋ณ‘ํ•ฉ์ •๋ ฌ -> ๋Œ€ํ‘œ์ ์ธ ๋ถ„ํ•  ์ •๋ณต(Divide and Conquer) ๋ฌธ์ œ
7+
* - ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ณ„์†ํ•ด์„œ ์ ์šฉํ•œ๋‹ค.
8+
* - ๋‘ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ๋ณ‘ํ•ฉํ•˜๋Š” ์˜ˆ์ œ
9+
* ใ„ด @link https://leetcode.com/problems/merge-two-sorted-lists/description/
10+
*
11+
*/
12+
/**
13+
* Definition for singly-linked list.
14+
* class ListNode {
15+
* val: number
16+
* next: ListNode | null
17+
* constructor(val?: number, next?: ListNode | null) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.next = (next===undefined ? null : next)
20+
* }
21+
* }
22+
*/
23+
24+
class ListNode {
25+
val: number;
26+
next: ListNode | null;
27+
constructor(val?: number, next?: ListNode | null) {
28+
this.val = val === undefined ? 0 : val;
29+
this.next = next === undefined ? null : next;
30+
}
31+
}
32+
33+
function mergeList(list1: ListNode | null, list2: ListNode | null) {
34+
const dummy = new ListNode(0);
35+
let current = dummy;
36+
37+
while (list1 && list2) {
38+
if (list1.val <= list2.val) {
39+
current.next = list1;
40+
list1 = list1.next;
41+
} else {
42+
current.next = list2;
43+
list2 = list2.next;
44+
}
45+
current = current.next;
46+
}
47+
current.next = list1 || list2;
48+
49+
return dummy.next;
50+
}
51+
52+
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
53+
return lists.reduce((acc, current) => {
54+
return mergeList(acc, current);
55+
}, null);
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ํšŒ์ „๋œ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ํƒ€๊ฒŸ ๊ฐ’์„ ์ฐพ๋Š” ๋ฌธ์ œ
4+
* - ์ด์ง„ ํƒ์ƒ‰์˜ ์‘์šฉ ๋ฒ„์ „
5+
*
6+
* ์•„์ด๋””์–ด
7+
* 1) ๋ณ€ํ˜•๋œ ์ด์ง„ ํƒ์ƒ‰ ์‚ฌ์šฉ
8+
* - ์ค‘๊ฐ„ ๊ฐ’๊ณผ ์™ผ์ชฝ, ๋ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์™ผ์ชฝ ์ •๋ ฌ ์˜์—ญ์ธ์ง€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ ์˜์—ญ์ธ์ง€ ํ™•์ธ
9+
*/
10+
11+
function search(nums: number[], target: number): number {
12+
let left = 0;
13+
let right = nums.length - 1;
14+
15+
while (left <= right) {
16+
const mid = Math.floor((left + right) / 2);
17+
18+
if (nums[mid] === target) return mid;
19+
20+
// mid๊ฐ€ ์™ผ์ชฝ ์ •๋ ฌ์— ํฌํ•จ
21+
if (nums[mid] >= nums[left]) {
22+
if (target >= nums[left] && target < nums[mid]) {
23+
right = mid - 1;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
// mid๊ฐ€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์— ํฌํ•จ
29+
else {
30+
if (target < nums[mid] && target <= nums[right]) {
31+
left = mid + 1;
32+
} else {
33+
right = mid - 1;
34+
}
35+
}
36+
}
37+
return -1;
38+
}

0 commit comments

Comments
ย (0)