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

[재호] WEEK 10 Solutions #530

Merged
merged 5 commits into from
Oct 21, 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
49 changes: 49 additions & 0 deletions course-schedule/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* TC: O(V + E)
* SC: O(V + E)
* N: numCourses(all of vertex), P: prerequisites(all of edge)
*/

/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function (numCourses, prerequisites) {
const STEP = {
before: 0,
ing: 1,
after: 2,
};
const stepBoard = Array.from({ length: numCourses }, () => STEP.before);
const board = Array.from({ length: numCourses }, () => []);

for (const [a, b] of prerequisites) {
board[a].push(b);
}

for (let index = 0; index < numCourses; index++) {
if (isCycle(index)) {
return false;
}
}
return true;

function isCycle(current) {
if (stepBoard[current] === STEP.end) {
return false;
}
if (stepBoard[current] === STEP.ing) {
return true;
}

stepBoard[current] = STEP.ing;
for (const next of board[current]) {
if (isCycle(next)) {
return true;
}
}
stepBoard[current] = STEP.end;
return false;
}
};
33 changes: 33 additions & 0 deletions invert-binary-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다.
*
* TC: O(N)
* 모든 트리를 순회합니다.
*
* SC: O(N)
* 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다.
Copy link
Contributor

Choose a reason for hiding this comment

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

트리의 높이 H로 공간 복잡도를 설명하는 것도 괜찮은 방법일 것 같습니다 :)

*
* N: tree의 모든 node 수
*/

/**
* 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) {
if (!root) {
return root;
}
[root.left, root.right] = [root.right, root.left];
invertTree(root.left);
invertTree(root.right);
return root;
};
37 changes: 37 additions & 0 deletions jump-game/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* TC: O(N)
* SC: O(1)
* N: nums.length
*/

/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function (nums) {
if (nums.length === 1) {
return true;
}

let maximumIndex = 0;

for (let index = 0; index < nums.length; index++) {
const jumpLength = nums[index];

if (jumpLength === 0) {
continue;
}

if (maximumIndex < index) {
return false;
}

maximumIndex = Math.max(maximumIndex, index + nums[index]);

if (maximumIndex >= nums.length - 1) {
return true;
}
}

return false;
};
65 changes: 65 additions & 0 deletions search-in-rotated-sorted-array/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 모든 케이스가 많지 않다고 생각되어 분기처리하였습니다..
Copy link
Contributor

Choose a reason for hiding this comment

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

생각하신 방법으로 끝까지 구현해내신게 멋집니다!

* 더 간결한 풀이법을 고려해보는 중..
*
* TC: O(log N)
* 이진탐색을 이용하여 순회합니다.
*
* SC: O(1)
* 이진탐색에 이용되는 투 포인터의 공간복잡도를 갖습니다.
*/

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

let left = 0;
let right = nums.length - 1;

while (left < right) {
const center = Math.floor((left + right) / 2);
if (target === nums[left]) {
return left;
}
if (target === nums[center]) {
return center;
}
if (target === nums[right]) {
return right;
}

if (nums[left] <= nums[center] && nums[center] < nums[right]) {
if (target < nums[left] || nums[right] < target) {
return -1;
} else if (nums[left] < target && target < nums[center]) {
right = center;
} else if (nums[center] < target && target < nums[right]) {
left = center + 1;
}
} else if (nums[right] < nums[left] && nums[left] <= nums[center]) {
if (nums[right] < target && target < nums[left]) {
return -1;
} else if (nums[left] < target && target < nums[center]) {
right = center;
} else {
left = center + 1;
}
} else if (nums[center] < nums[right] && nums[right] < nums[left]) {
if (nums[center] < target && target < nums[right]) {
left = center + 1;
} else if (nums[right] < target && target < nums[left]) {
return -1;
} else {
right = center;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

직접 하나씩 분기처리하여 답을 찾아내신점 정말 멋집니다!
아래는 중요하지 않은 내용입니다
53-61라인의 경우에도 37-44, 45-52라인과 같이 대칭성을 맞춰주셨다면 조금 더 좋았을것같네요!

}

return -1;
};