Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc5692c

Browse files
authoredJun 6, 2025
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)
Please sign in to comment.