Skip to content

Commit b87290d

Browse files
authored
Merge pull request #1008 from gwbaik9717/main
[ganu] Week 10
2 parents 3963288 + 27fac22 commit b87290d

File tree

5 files changed

+303
-0
lines changed

5 files changed

+303
-0
lines changed

course-schedule/gwbaik9717.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// n: numCourses, p: len(prerequisites)
2+
// Time complexity: O(n + p)
3+
// Space complexity: O(n + p)
4+
5+
class _Queue {
6+
constructor() {
7+
this.q = [];
8+
this.start = 0;
9+
this.end = 0;
10+
}
11+
12+
isEmpty() {
13+
return this.start === this.end;
14+
}
15+
16+
push(value) {
17+
this.q.push(value);
18+
this.end++;
19+
}
20+
21+
shift() {
22+
const rv = this.q[this.start];
23+
delete this.q[this.start++];
24+
25+
return rv;
26+
}
27+
}
28+
29+
/**
30+
* @param {number} numCourses
31+
* @param {number[][]} prerequisites
32+
* @return {boolean}
33+
*/
34+
var canFinish = function (numCourses, prerequisites) {
35+
const graph = Array.from({ length: numCourses }, () => []);
36+
const inDegree = Array.from({ length: numCourses }, () => 0);
37+
38+
for (const [end, start] of prerequisites) {
39+
graph[start].push(end);
40+
inDegree[end]++;
41+
}
42+
43+
const q = new _Queue();
44+
45+
for (let i = 0; i < numCourses; i++) {
46+
if (inDegree[i] === 0) {
47+
q.push(i);
48+
}
49+
}
50+
51+
let count = 0;
52+
53+
while (!q.isEmpty()) {
54+
const current = q.shift();
55+
count++;
56+
57+
// 현재 노드와 연결된 다른 노드의 차수 감소
58+
for (const node of graph[current]) {
59+
inDegree[node] -= 1;
60+
61+
if (inDegree[node] === 0) {
62+
q.push(node);
63+
}
64+
}
65+
}
66+
67+
return count === numCourses;
68+
};

invert-binary-tree/gwbaik9717.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {TreeNode}
15+
*/
16+
var invertTree = function (root) {
17+
const dfs = (current) => {
18+
if (!current) {
19+
return;
20+
}
21+
22+
const temp = current.left;
23+
current.left = current.right;
24+
current.right = temp;
25+
26+
dfs(current.left);
27+
dfs(current.right);
28+
};
29+
30+
dfs(root);
31+
32+
return root;
33+
};

jump-game/gwbaik9717.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {boolean}
7+
*/
8+
var canJump = function (nums) {
9+
let maxValue = nums[0];
10+
11+
for (let i = 1; i < nums.length; i++) {
12+
if (i > maxValue) {
13+
return false;
14+
}
15+
16+
const newNum = i + nums[i];
17+
18+
if (newNum > maxValue) {
19+
maxValue = newNum;
20+
}
21+
22+
if (maxValue >= nums.length - 1) {
23+
break;
24+
}
25+
}
26+
27+
return true;
28+
};

merge-k-sorted-lists/gwbaik9717.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// k: len(lists), n: Total number of nodes
2+
// Time complexity: O(nlogk)
3+
// Space complexity: O(n)
4+
5+
class MinHeap {
6+
constructor() {
7+
this.heap = [null];
8+
}
9+
10+
isEmpty() {
11+
return this.heap.length === 1;
12+
}
13+
14+
push(listNode) {
15+
this.heap.push(listNode);
16+
17+
let current = this.heap.length - 1;
18+
let parent = Math.floor(current / 2);
19+
20+
while (parent !== 0) {
21+
if (this.heap[parent].val > listNode.val) {
22+
[this.heap[parent], this.heap[current]] = [
23+
this.heap[current],
24+
this.heap[parent],
25+
];
26+
27+
current = parent;
28+
parent = Math.floor(current / 2);
29+
continue;
30+
}
31+
32+
break;
33+
}
34+
}
35+
36+
pop() {
37+
if (this.heap.length === 1) {
38+
return;
39+
}
40+
41+
if (this.heap.length === 2) {
42+
return this.heap.pop();
43+
}
44+
45+
const rv = this.heap[1];
46+
47+
this.heap[1] = this.heap.pop();
48+
49+
let current = 1;
50+
let left = current * 2;
51+
let right = left + 1;
52+
53+
while (
54+
(this.heap[left] && this.heap[current].val > this.heap[left].val) ||
55+
(this.heap[right] && this.heap[current].val > this.heap[right].val)
56+
) {
57+
if (this.heap[left] && this.heap[right]) {
58+
if (this.heap[left].val < this.heap[right].val) {
59+
[this.heap[left], this.heap[current]] = [
60+
this.heap[current],
61+
this.heap[left],
62+
];
63+
current = left;
64+
} else {
65+
[this.heap[right], this.heap[current]] = [
66+
this.heap[current],
67+
this.heap[right],
68+
];
69+
current = right;
70+
}
71+
72+
left = current * 2;
73+
right = left + 1;
74+
continue;
75+
}
76+
77+
[this.heap[left], this.heap[current]] = [
78+
this.heap[current],
79+
this.heap[left],
80+
];
81+
current = left;
82+
left = current * 2;
83+
right = left + 1;
84+
}
85+
86+
return rv;
87+
}
88+
}
89+
90+
/**
91+
* Definition for singly-linked list.
92+
* function ListNode(val, next) {
93+
* this.val = (val===undefined ? 0 : val)
94+
* this.next = (next===undefined ? null : next)
95+
* }
96+
*/
97+
/**
98+
* @param {ListNode[]} lists
99+
* @return {ListNode}
100+
*/
101+
var mergeKLists = function (lists) {
102+
const minHeap = new MinHeap();
103+
104+
for (const list of lists) {
105+
if (list) {
106+
minHeap.push(list);
107+
}
108+
}
109+
110+
let answer = null;
111+
let next = null;
112+
113+
while (!minHeap.isEmpty()) {
114+
const current = minHeap.pop();
115+
116+
if (!answer) {
117+
answer = new ListNode();
118+
next = answer;
119+
}
120+
121+
next.val = current.val;
122+
123+
if (current.next) {
124+
minHeap.push(current.next);
125+
}
126+
127+
if (!minHeap.isEmpty()) {
128+
next.next = new ListNode();
129+
next = next.next;
130+
}
131+
}
132+
133+
return answer;
134+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Time complexity: O(logn)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @param {number} target
7+
* @return {number}
8+
*/
9+
var search = function (nums, target) {
10+
let left = 0;
11+
let right = nums.length - 1;
12+
13+
while (left <= right) {
14+
const mid = Math.floor((left + right) / 2);
15+
16+
if (nums.at(mid) === target) {
17+
return mid;
18+
}
19+
20+
// rotate 된 구간이 있을 때
21+
if (nums.at(mid + 1) > nums.at(right)) {
22+
if (nums.at(right) >= target || nums.at(mid + 1) <= target) {
23+
left = mid + 1;
24+
continue;
25+
}
26+
27+
right = mid - 1;
28+
continue;
29+
}
30+
31+
if (target >= nums.at(mid + 1) && target <= nums.at(right)) {
32+
left = mid + 1;
33+
continue;
34+
}
35+
36+
right = mid - 1;
37+
}
38+
39+
return -1;
40+
};

0 commit comments

Comments
 (0)