-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다. | ||
* | ||
* TC: O(N) | ||
* 모든 트리를 순회합니다. | ||
* | ||
* SC: O(N) | ||
* 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다. | ||
* | ||
* 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 모든 케이스가 많지 않다고 생각되어 분기처리하였습니다.. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 직접 하나씩 분기처리하여 답을 찾아내신점 정말 멋집니다! |
||
} | ||
|
||
return -1; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
트리의 높이 H로 공간 복잡도를 설명하는 것도 괜찮은 방법일 것 같습니다 :)