Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ganu] Week 10 #1008

Merged
merged 5 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions course-schedule/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// n: numCourses, p: len(prerequisites)
// Time complexity: O(n + p)
// Space complexity: O(n + p)

class _Queue {
constructor() {
this.q = [];
this.start = 0;
this.end = 0;
}

isEmpty() {
return this.start === this.end;
}

push(value) {
this.q.push(value);
this.end++;
}

shift() {
const rv = this.q[this.start];
delete this.q[this.start++];

return rv;
}
}

/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function (numCourses, prerequisites) {
const graph = Array.from({ length: numCourses }, () => []);
const inDegree = Array.from({ length: numCourses }, () => 0);

for (const [end, start] of prerequisites) {
graph[start].push(end);
inDegree[end]++;
}

const q = new _Queue();

for (let i = 0; i < numCourses; i++) {
if (inDegree[i] === 0) {
q.push(i);
}
}

let count = 0;

while (!q.isEmpty()) {
const current = q.shift();
count++;

// 현재 노드와 연결된 다른 노드의 차수 감소
for (const node of graph[current]) {
inDegree[node] -= 1;

if (inDegree[node] === 0) {
q.push(node);
}
}
}

return count === numCourses;
};
33 changes: 33 additions & 0 deletions invert-binary-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
const dfs = (current) => {
if (!current) {
return;
}

const temp = current.left;
current.left = current.right;
current.right = temp;

dfs(current.left);
dfs(current.right);
};

dfs(root);

return root;
};
Comment on lines +12 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DFS와 재귀를 활용하면 더 깔끔한 코드로 구현할 수 있겠어요. 참고가 되었습니다. 감사합니다!

28 changes: 28 additions & 0 deletions jump-game/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time complexity: O(n)
// Space complexity: O(1)
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 거의 동일한 방식으로 풀었습니다! 표현하는 방식 등 여러가지로 파이썬과 다른 부분이 있어서 비교해서 보는 재미가 있었어요. 시간/공간 복잡도에 대한 설명을 추가해주시면 더 좋을 것 같습니다. 이번 주도 고생하셨습니다!


/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function (nums) {
let maxValue = nums[0];

for (let i = 1; i < nums.length; i++) {
if (i > maxValue) {
return false;
}

const newNum = i + nums[i];

if (newNum > maxValue) {
maxValue = newNum;
}

if (maxValue >= nums.length - 1) {
break;
}
}

return true;
};
134 changes: 134 additions & 0 deletions merge-k-sorted-lists/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// k: len(lists), n: Total number of nodes
// Time complexity: O(nlogk)
// Space complexity: O(n)

class MinHeap {
constructor() {
this.heap = [null];
}

isEmpty() {
return this.heap.length === 1;
}

push(listNode) {
this.heap.push(listNode);

let current = this.heap.length - 1;
let parent = Math.floor(current / 2);

while (parent !== 0) {
if (this.heap[parent].val > listNode.val) {
[this.heap[parent], this.heap[current]] = [
this.heap[current],
this.heap[parent],
];

current = parent;
parent = Math.floor(current / 2);
continue;
}

break;
}
}

pop() {
if (this.heap.length === 1) {
return;
}

if (this.heap.length === 2) {
return this.heap.pop();
}

const rv = this.heap[1];

this.heap[1] = this.heap.pop();

let current = 1;
let left = current * 2;
let right = left + 1;

while (
(this.heap[left] && this.heap[current].val > this.heap[left].val) ||
(this.heap[right] && this.heap[current].val > this.heap[right].val)
) {
if (this.heap[left] && this.heap[right]) {
if (this.heap[left].val < this.heap[right].val) {
[this.heap[left], this.heap[current]] = [
this.heap[current],
this.heap[left],
];
current = left;
} else {
[this.heap[right], this.heap[current]] = [
this.heap[current],
this.heap[right],
];
current = right;
}

left = current * 2;
right = left + 1;
continue;
}

[this.heap[left], this.heap[current]] = [
this.heap[current],
this.heap[left],
];
current = left;
left = current * 2;
right = left + 1;
}

return rv;
}
}

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
const minHeap = new MinHeap();

for (const list of lists) {
if (list) {
minHeap.push(list);
}
}

let answer = null;
let next = null;

while (!minHeap.isEmpty()) {
const current = minHeap.pop();

if (!answer) {
answer = new ListNode();
next = answer;
}

next.val = current.val;

if (current.next) {
minHeap.push(current.next);
}

if (!minHeap.isEmpty()) {
next.next = new ListNode();
next = next.next;
}
}

return answer;
};
40 changes: 40 additions & 0 deletions search-in-rotated-sorted-array/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time complexity: O(logn)
// Space complexity: O(1)

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {
let left = 0;
let right = nums.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);

if (nums.at(mid) === target) {
return mid;
}

// rotate 된 구간이 있을 때
if (nums.at(mid + 1) > nums.at(right)) {
if (nums.at(right) >= target || nums.at(mid + 1) <= target) {
left = mid + 1;
continue;
}

right = mid - 1;
continue;
}

if (target >= nums.at(mid + 1) && target <= nums.at(right)) {
left = mid + 1;
continue;
}

right = mid - 1;
}

return -1;
};