Skip to content

Commit d7cf0b1

Browse files
authored
Merge pull request #1554 from Moonjonghoo/main
[moonjonghoo] WEEK 10 Solutions
2 parents 2655ed1 + 82cef7c commit d7cf0b1

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var canFinish = function (numCourses, prerequisites) {
2+
const graph = Array.from({ length: numCourses }, () => []);
3+
const inDegree = Array(numCourses).fill(0);
4+
5+
// ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ ๋ฐ ์ง„์ž… ์ฐจ์ˆ˜ ๊ณ„์‚ฐ
6+
for (const [course, pre] of prerequisites) {
7+
graph[pre].push(course);
8+
inDegree[course]++;
9+
}
10+
11+
// ์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘
12+
const queue = [];
13+
for (let i = 0; i < numCourses; i++) {
14+
if (inDegree[i] === 0) queue.push(i);
15+
}
16+
17+
let count = 0;
18+
while (queue.length) {
19+
const node = queue.shift();
20+
count++;
21+
22+
for (const neighbor of graph[node]) {
23+
inDegree[neighbor]--;
24+
if (inDegree[neighbor] === 0) {
25+
queue.push(neighbor);
26+
}
27+
}
28+
}
29+
30+
return count === numCourses;
31+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var invertTree = function (root) {
2+
if (root === null) return null;
3+
4+
let queue = [root];
5+
6+
while (queue.length > 0) {
7+
let node = queue.shift();
8+
9+
// ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๊ตํ™˜
10+
[node.left, node.right] = [node.right, node.left];
11+
12+
if (node.left) queue.push(node.left);
13+
if (node.right) queue.push(node.right);
14+
}
15+
16+
return root;
17+
};

โ€Žjump-game/moonjonghoo.jsโ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var canJump = function (nums) {
2+
let maxReach = 0;
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (i > maxReach) return false;
6+
maxReach = Math.max(maxReach, i + nums[i]);
7+
}
8+
9+
return true;
10+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var search = function (nums, target) {
2+
let left = 0,
3+
right = nums.length - 1;
4+
5+
while (left <= right) {
6+
const mid = Math.floor((left + right) / 2);
7+
8+
if (nums[mid] === target) return mid;
9+
10+
// ์™ผ์ชฝ ์ ˆ๋ฐ˜์ด ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ
11+
if (nums[left] <= nums[mid]) {
12+
if (nums[left] <= target && target < nums[mid]) {
13+
right = mid - 1; // ์™ผ์ชฝ ๋ฒ”์œ„๋กœ ์ด๋™
14+
} else {
15+
left = mid + 1; // ์˜ค๋ฅธ์ชฝ ๋ฒ”์œ„๋กœ ์ด๋™
16+
}
17+
}
18+
// ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์ด ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ
19+
else {
20+
if (nums[mid] < target && target <= nums[right]) {
21+
left = mid + 1; // ์˜ค๋ฅธ์ชฝ ๋ฒ”์œ„๋กœ ์ด๋™
22+
} else {
23+
right = mid - 1; // ์™ผ์ชฝ ๋ฒ”์œ„๋กœ ์ด๋™
24+
}
25+
}
26+
}
27+
28+
return -1;
29+
};

0 commit comments

Comments
ย (0)