Skip to content

Commit afb1e8a

Browse files
authored
Merge pull request #537 from Sunjae95/main
[선재] WEEK10 Solutions
2 parents 796ade7 + cfa4c54 commit afb1e8a

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

course-schedule/sunjae95.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @description
3+
* memoization + dfs
4+
*
5+
* n = length of nums
6+
* p = length of prerequisites
7+
*
8+
* time complexity: O(n)
9+
* space complexity: O(p)
10+
*/
11+
var canFinish = function (numCourses, prerequisites) {
12+
const memo = Array.from({ length: numCourses + 1 }, () => false);
13+
const visited = Array.from({ length: numCourses + 1 }, () => false);
14+
// graph setting
15+
const graph = prerequisites.reduce((map, [linkedNode, current]) => {
16+
const list = map.get(current) ?? [];
17+
list.push(linkedNode);
18+
map.set(current, list);
19+
return map;
20+
}, new Map());
21+
22+
const dfs = (current) => {
23+
const linkedNode = graph.get(current);
24+
25+
if (memo[current] || !linkedNode || linkedNode.length === 0) return true;
26+
27+
for (const node of linkedNode) {
28+
if (visited[node]) return false;
29+
30+
visited[node] = true;
31+
if (!dfs(node)) return false;
32+
visited[node] = false;
33+
memo[node] = true;
34+
}
35+
36+
return true;
37+
};
38+
39+
for (const [current] of graph) {
40+
visited[current] = true;
41+
if (!dfs(current)) return false;
42+
visited[current] = false;
43+
memo[current] = true;
44+
}
45+
46+
return true;
47+
};

invert-binary-tree/sunjae95.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* preorder traverse
5+
*
6+
* n = length of root
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var invertTree = function (root) {
11+
const preOrder = (tree) => {
12+
if (tree === null) return null;
13+
14+
const currentNode = new TreeNode(tree.val);
15+
16+
currentNode.right = preOrder(tree.left);
17+
currentNode.left = preOrder(tree.right);
18+
19+
return currentNode;
20+
};
21+
22+
return preOrder(root);
23+
};

jump-game/sunjae95.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @description
3+
*
4+
* n = length of nums
5+
* time complexity: O(n)
6+
* space complexity: O(1)
7+
*/
8+
var canJump = function (nums) {
9+
let cur = 1;
10+
for (const num of nums) {
11+
if (cur === 0) return false;
12+
cur--;
13+
cur = cur > num ? cur : num;
14+
}
15+
16+
return true;
17+
};

merge-k-sorted-lists/sunjae95.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @description
3+
* queue의 특성을 활용하여 풀이
4+
*
5+
* n = length of lists
6+
* m = length of lists[i]
7+
* time complexity: O(n * n * m)
8+
* space complexity: O(n*m)
9+
*/
10+
var mergeKLists = function (lists) {
11+
let answer = null;
12+
let tail = null;
13+
let totalSize = lists.reduce((size, list) => {
14+
let head = list;
15+
let count = 0;
16+
17+
while (head) {
18+
head = head.next;
19+
count++;
20+
}
21+
22+
return size + count;
23+
}, 0);
24+
25+
while (totalSize--) {
26+
let minIndex = lists.reduce((acc, list, i) => {
27+
if (list === null) return acc;
28+
if (acc === null) return { value: list.val, index: i };
29+
return acc.value < list.val ? acc : { value: list.val, index: i };
30+
}, null).index;
31+
32+
if (answer === null) {
33+
answer = lists[minIndex];
34+
tail = answer;
35+
} else {
36+
tail.next = lists[minIndex];
37+
tail = lists[minIndex];
38+
}
39+
40+
lists[minIndex] = lists[minIndex].next;
41+
}
42+
return answer;
43+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
4+
* n = length of nums
5+
* time complexity: O(n)
6+
* space complexity: O(1)
7+
*/
8+
var search = function (nums, target) {
9+
let [start, end] = [0, nums.length - 1];
10+
let answer = -1;
11+
12+
while (start !== end) {
13+
if (nums[start] === target) answer = start;
14+
if (nums[end] === target) answer = end;
15+
if (nums[start] > nums[end]) end--;
16+
else start++;
17+
}
18+
19+
if (nums[start] === target) answer = start;
20+
21+
return answer;
22+
};

0 commit comments

Comments
 (0)