-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from wogha95/main
[μ¬νΈ] WEEK 10 Solutions
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
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 @@ | ||
/** | ||
* λͺ¨λ μΌμ΄μ€κ° λ§μ§ μλ€κ³ μκ°λμ΄ λΆκΈ°μ²λ¦¬νμμ΅λλ€.. | ||
* λ κ°κ²°ν νμ΄λ²μ κ³ λ €ν΄λ³΄λ μ€.. | ||
* | ||
* 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; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
}; |