Skip to content

Commit de7ae70

Browse files
authored
Merge pull request #1544 from byol-han/main
[byol-han] WEEK 10 solutions
2 parents ce73260 + b57c9b3 commit de7ae70

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

course-schedule/byol-han.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/course-schedule/description/
3+
* @param {number} numCourses
4+
* @param {number[][]} prerequisites
5+
* @return {boolean}
6+
*/
7+
var canFinish = function (numCourses, prerequisites) {
8+
if (numCourses === 1) return true; // 과목이 1개면 무조건 수강 가능
9+
10+
let adjList = new Map(); // 각 과목에 대해 선행 과목들을 저장하는 인접 리스트
11+
let visited = new Set(); // 이미 수강 가능한 것으로 확인된 과목들
12+
let path = new Set(); // 현재 DFS 경로에 포함된 과목들 (사이클 확인용)
13+
14+
// 모든 과목을 인접 리스트에 초기화
15+
for (let i = 0; i < numCourses; i++) {
16+
adjList.set(i, []);
17+
}
18+
19+
// prerequisite 정보를 기반으로 인접 리스트 구성
20+
for (let i = 0; i < prerequisites.length; i++) {
21+
// [0, 1]은 0을 듣기 위해 1을 먼저 들어야 함
22+
adjList.get(prerequisites[i][0]).push(prerequisites[i][1]);
23+
}
24+
25+
// DFS 함수: 해당 course를 수강할 수 있는지 확인
26+
const canTake = (course) => {
27+
if (visited.has(course)) return true; // 이미 확인된 코스는 재확인 필요 없음
28+
if (path.has(course)) return false; // 현재 DFS 경로에 course가 있다면 사이클 발생
29+
30+
path.add(course); // 현재 경로에 course 추가
31+
32+
let preCourses = adjList.get(course); // course를 듣기 위한 선행 과목들
33+
for (let i = 0; i < preCourses.length; i++) {
34+
if (!canTake(preCourses[i])) return false; // 선행 과목 중 하나라도 불가능하면 종료
35+
}
36+
37+
path.delete(course); // 경로에서 제거 (백트래킹)
38+
visited.add(course); // 수강 가능한 것으로 확인
39+
return true;
40+
};
41+
42+
// 모든 과목에 대해 수강 가능한지 확인
43+
for (let i = 0; i < numCourses; i++) {
44+
path = new Set(); // 새로운 DFS 탐색이므로 경로 초기화
45+
if (!canTake(i)) return false; // 한 과목이라도 못 들으면 false
46+
}
47+
48+
return true; // 모든 과목 수강 가능
49+
};

invert-binary-tree/byol-han.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/invert-binary-tree/submissions/1651537941/
3+
* Definition for a binary tree node.
4+
* function TreeNode(val, left, right) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.left = (left===undefined ? null : left)
7+
* this.right = (right===undefined ? null : right)
8+
* }
9+
*/
10+
/**
11+
* @param {TreeNode} root
12+
* @return {TreeNode}
13+
*/
14+
var invertTree = function (root) {
15+
if (root === null) return null;
16+
17+
// 왼쪽과 오른쪽 자식 노드 바꾸기
18+
[root.left, root.right] = [invertTree(root.right), invertTree(root.left)];
19+
20+
return root;
21+
};

jump-game/byol-han.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/jump-game/description/
3+
* @param {number[]} nums
4+
* @return {boolean}
5+
*/
6+
var canJump = function (nums) {
7+
let maxReach = 0; // 현재까지 도달할 수 있는 최대 인덱스
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
// 현재 위치가 도달 가능한 최대 거리보다 멀다면 끝에 도달할 수 없음
11+
if (i > maxReach) {
12+
return false;
13+
}
14+
15+
// 현재 위치에서 갈 수 있는 최대 위치 업데이트
16+
maxReach = Math.max(maxReach, i + nums[i]);
17+
18+
// 최종 목적지에 도달 가능하면 true 바로 반환
19+
if (maxReach >= nums.length - 1) {
20+
return true;
21+
}
22+
}
23+
24+
// 반복문 끝나도 못 도달한 경우
25+
return false;
26+
};

merge-k-sorted-lists/byol-han.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/merge-k-sorted-lists/description/
3+
* Definition for singly-linked list.
4+
* function ListNode(val, next) {
5+
* this.val = (val===undefined ? 0 : val)
6+
* this.next = (next===undefined ? null : next)
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode[]} lists
11+
* @return {ListNode}
12+
*/
13+
var mergeKLists = function (lists) {
14+
if (lists.length === 0) {
15+
return null;
16+
}
17+
18+
while (lists.length > 1) {
19+
const merged = [];
20+
const size = lists.length;
21+
22+
for (let i = 0; i < size; i += 2) {
23+
const l1 = lists[i];
24+
const l2 = i + 1 < lists.length ? lists[i + 1] : null;
25+
merged.push(mergeLists(l1, l2));
26+
}
27+
28+
lists = merged;
29+
}
30+
31+
return lists[0];
32+
};
33+
34+
function mergeLists(l1, l2) {
35+
const head = new ListNode(0);
36+
let cur = head;
37+
while (l1 !== null && l2 !== null) {
38+
if (l1.val < l2.val) {
39+
cur.next = l1;
40+
l1 = l1.next;
41+
} else {
42+
cur.next = l2;
43+
l2 = l2.next;
44+
}
45+
cur = cur.next;
46+
}
47+
cur.next = l1 === null ? l2 : l1;
48+
return head.next;
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* https://leetcode.com/problems/search-in-rotated-sorted-array/
3+
* @param {number[]} nums
4+
* @param {number} target
5+
* @return {number}
6+
*/
7+
var search = function (nums, target) {
8+
let left = 0;
9+
let right = nums.length - 1;
10+
11+
// Continue searching while the window is valid
12+
while (left <= right) {
13+
// Find the middle index
14+
let mid = Math.floor((left + right) / 2);
15+
16+
// If the target is found at mid, return the index
17+
if (nums[mid] === target) {
18+
return mid;
19+
}
20+
21+
// Check if the left half is sorted
22+
if (nums[left] <= nums[mid]) {
23+
// If target is in the range of the sorted left half
24+
if (nums[left] <= target && target < nums[mid]) {
25+
// Move the right pointer to just before mid
26+
right = mid - 1;
27+
} else {
28+
// Target must be in the right half
29+
left = mid + 1;
30+
}
31+
32+
// Otherwise, the right half must be sorted
33+
} else {
34+
// If target is in the range of the sorted right half
35+
if (nums[mid] < target && target <= nums[right]) {
36+
// Move the left pointer to just after mid
37+
left = mid + 1;
38+
} else {
39+
// Target must be in the left half
40+
right = mid - 1;
41+
}
42+
}
43+
}
44+
45+
// If the loop ends, target is not in the array
46+
return -1;
47+
};

0 commit comments

Comments
 (0)