Skip to content

Commit b14e4d9

Browse files
authored
Merge pull request #530 from wogha95/main
[재호] WEEK 10 Solutions
2 parents bd5bb4f + 42c0ba4 commit b14e4d9

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

course-schedule/wogha95.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* TC: O(V + E)
3+
* SC: O(V + E)
4+
* N: numCourses(all of vertex), P: prerequisites(all of edge)
5+
*/
6+
7+
/**
8+
* @param {number} numCourses
9+
* @param {number[][]} prerequisites
10+
* @return {boolean}
11+
*/
12+
var canFinish = function (numCourses, prerequisites) {
13+
const STEP = {
14+
before: 0,
15+
ing: 1,
16+
after: 2,
17+
};
18+
const stepBoard = Array.from({ length: numCourses }, () => STEP.before);
19+
const board = Array.from({ length: numCourses }, () => []);
20+
21+
for (const [a, b] of prerequisites) {
22+
board[a].push(b);
23+
}
24+
25+
for (let index = 0; index < numCourses; index++) {
26+
if (isCycle(index)) {
27+
return false;
28+
}
29+
}
30+
return true;
31+
32+
function isCycle(current) {
33+
if (stepBoard[current] === STEP.end) {
34+
return false;
35+
}
36+
if (stepBoard[current] === STEP.ing) {
37+
return true;
38+
}
39+
40+
stepBoard[current] = STEP.ing;
41+
for (const next of board[current]) {
42+
if (isCycle(next)) {
43+
return true;
44+
}
45+
}
46+
stepBoard[current] = STEP.end;
47+
return false;
48+
}
49+
};

invert-binary-tree/wogha95.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다.
3+
*
4+
* TC: O(N)
5+
* 모든 트리를 순회합니다.
6+
*
7+
* SC: O(N)
8+
* 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다.
9+
*
10+
* N: tree의 모든 node 수
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
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+
* @param {TreeNode} root
23+
* @return {TreeNode}
24+
*/
25+
var invertTree = function (root) {
26+
if (!root) {
27+
return root;
28+
}
29+
[root.left, root.right] = [root.right, root.left];
30+
invertTree(root.left);
31+
invertTree(root.right);
32+
return root;
33+
};

jump-game/wogha95.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(1)
4+
* N: nums.length
5+
*/
6+
7+
/**
8+
* @param {number[]} nums
9+
* @return {boolean}
10+
*/
11+
var canJump = function (nums) {
12+
if (nums.length === 1) {
13+
return true;
14+
}
15+
16+
let maximumIndex = 0;
17+
18+
for (let index = 0; index < nums.length; index++) {
19+
const jumpLength = nums[index];
20+
21+
if (jumpLength === 0) {
22+
continue;
23+
}
24+
25+
if (maximumIndex < index) {
26+
return false;
27+
}
28+
29+
maximumIndex = Math.max(maximumIndex, index + nums[index]);
30+
31+
if (maximumIndex >= nums.length - 1) {
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
};
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* 모든 케이스가 많지 않다고 생각되어 분기처리하였습니다..
3+
* 더 간결한 풀이법을 고려해보는 중..
4+
*
5+
* TC: O(log N)
6+
* 이진탐색을 이용하여 순회합니다.
7+
*
8+
* SC: O(1)
9+
* 이진탐색에 이용되는 투 포인터의 공간복잡도를 갖습니다.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} target
15+
* @return {number}
16+
*/
17+
var search = function (nums, target) {
18+
if (nums.length === 1) {
19+
return target === nums[0] ? 0 : -1;
20+
}
21+
22+
let left = 0;
23+
let right = nums.length - 1;
24+
25+
while (left < right) {
26+
const center = Math.floor((left + right) / 2);
27+
if (target === nums[left]) {
28+
return left;
29+
}
30+
if (target === nums[center]) {
31+
return center;
32+
}
33+
if (target === nums[right]) {
34+
return right;
35+
}
36+
37+
if (nums[left] <= nums[center] && nums[center] < nums[right]) {
38+
if (target < nums[left] || nums[right] < target) {
39+
return -1;
40+
} else if (nums[left] < target && target < nums[center]) {
41+
right = center;
42+
} else if (nums[center] < target && target < nums[right]) {
43+
left = center + 1;
44+
}
45+
} else if (nums[right] < nums[left] && nums[left] <= nums[center]) {
46+
if (nums[right] < target && target < nums[left]) {
47+
return -1;
48+
} else if (nums[left] < target && target < nums[center]) {
49+
right = center;
50+
} else {
51+
left = center + 1;
52+
}
53+
} else if (nums[center] < nums[right] && nums[right] < nums[left]) {
54+
if (nums[center] < target && target < nums[right]) {
55+
left = center + 1;
56+
} else if (nums[right] < target && target < nums[left]) {
57+
return -1;
58+
} else {
59+
right = center;
60+
}
61+
}
62+
}
63+
64+
return -1;
65+
};

0 commit comments

Comments
 (0)