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

[선재] WEEK10 Solutions #537

Merged
merged 5 commits into from
Oct 19, 2024
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
47 changes: 47 additions & 0 deletions course-schedule/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @description
* memoization + dfs
*
* n = length of nums
* p = length of prerequisites
*
* time complexity: O(n)
* space complexity: O(p)
*/
var canFinish = function (numCourses, prerequisites) {
const memo = Array.from({ length: numCourses + 1 }, () => false);
Copy link
Contributor

Choose a reason for hiding this comment

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

와 처음에는 메모가 과연 효과가 있을까..? 생각했는데 몇번을 돌려도 Beats 100%네요 👍

image

const visited = Array.from({ length: numCourses + 1 }, () => false);
// graph setting
const graph = prerequisites.reduce((map, [linkedNode, current]) => {
const list = map.get(current) ?? [];
list.push(linkedNode);
map.set(current, list);
return map;
}, new Map());

const dfs = (current) => {
const linkedNode = graph.get(current);

if (memo[current] || !linkedNode || linkedNode.length === 0) return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분은 옵셔널 체이닝을 사용하시면 조금 더 간결해질 것 같아요!
if (memo[current] || linkedNode?.length){ ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오 그렇네요🙌


for (const node of linkedNode) {
if (visited[node]) return false;

visited[node] = true;
if (!dfs(node)) return false;
visited[node] = false;
memo[node] = true;
}

return true;
};

for (const [current] of graph) {
visited[current] = true;
if (!dfs(current)) return false;
visited[current] = false;
memo[current] = true;
}

return true;
};
23 changes: 23 additions & 0 deletions invert-binary-tree/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @description
* brainstorming:
* preorder traverse
*
* n = length of root
Copy link
Contributor

Choose a reason for hiding this comment

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

length of root는 전체 노드의 수를 말씀하시는걸까요?

Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 length of root은 무엇을 의미하는 것일까요?

* time complexity: O(n)
* space complexity: O(n)
*/
var invertTree = function (root) {
const preOrder = (tree) => {
if (tree === null) return null;

const currentNode = new TreeNode(tree.val);

currentNode.right = preOrder(tree.left);
currentNode.left = preOrder(tree.right);

return currentNode;
};

return preOrder(root);
};
17 changes: 17 additions & 0 deletions jump-game/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @description
*
* n = length of nums
* time complexity: O(n)
* space complexity: O(1)
*/
var canJump = function (nums) {
let cur = 1;
for (const num of nums) {
if (cur === 0) return false;
cur--;
cur = cur > num ? cur : num;
}

return true;
};
43 changes: 43 additions & 0 deletions merge-k-sorted-lists/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @description
* queue의 특성을 활용하여 풀이
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분에 대해서 혹시 추가 설명을 요청드려도 될까요?

*
* n = length of lists
* m = length of lists[i]
* time complexity: O(n * n * m)
* space complexity: O(n*m)
*/
var mergeKLists = function (lists) {
let answer = null;
let tail = null;
let totalSize = lists.reduce((size, list) => {
let head = list;
let count = 0;

while (head) {
head = head.next;
count++;
}

return size + count;
}, 0);

while (totalSize--) {
let minIndex = lists.reduce((acc, list, i) => {
if (list === null) return acc;
if (acc === null) return { value: list.val, index: i };
return acc.value < list.val ? acc : { value: list.val, index: i };
}, null).index;

if (answer === null) {
answer = lists[minIndex];
tail = answer;
} else {
tail.next = lists[minIndex];
tail = lists[minIndex];
}
Comment on lines +32 to +38
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HC-kang
매번 lists의 각 lists의 head중 가장 작은 index의 head를 바꿔주는방법이 queue의 선입선출을 연상되어서 적었습니다. 🙂


lists[minIndex] = lists[minIndex].next;
}
return answer;
};
22 changes: 22 additions & 0 deletions search-in-rotated-sorted-array/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @description
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
* n = length of nums
* time complexity: O(n)
* space complexity: O(1)
*/
var search = function (nums, target) {
let [start, end] = [0, nums.length - 1];
let answer = -1;

while (start !== end) {
if (nums[start] === target) answer = start;
if (nums[end] === target) answer = end;
if (nums[start] > nums[end]) end--;
else start++;
}

if (nums[start] === target) answer = start;

return answer;
};