-
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 #597 from Sunjae95/main
[μ μ¬] Week14
- Loading branch information
Showing
3 changed files
with
80 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,31 @@ | ||
/** | ||
* @description | ||
* λμΌν depthλ₯Ό λ°©λ¬Έν΄μΌνλ―λ‘ bfs λ° νΈλ¦¬ μν | ||
* | ||
* n = length of node of root | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var levelOrder = function (root) { | ||
if (!root) return []; | ||
|
||
const answer = []; | ||
const queue = [root]; | ||
let queueCurrentIndex = 0; | ||
|
||
while (queue.length > queueCurrentIndex) { | ||
answer.push([]); | ||
const answerLastIndex = answer.length - 1; | ||
const depthEndIndex = queue.length; | ||
|
||
while (depthEndIndex !== queueCurrentIndex) { | ||
const tree = queue[queueCurrentIndex++]; | ||
|
||
answer[answerLastIndex].push(tree.val); | ||
if (tree.left) queue.push(tree.left); | ||
if (tree.right) queue.push(tree.right); | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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,31 @@ | ||
/** | ||
* @description | ||
* μ νμ: dp[i] = Math.max(dp[i - 1], dp[i - 2] + current); | ||
* μννλ€λ μ‘°κ±΄μ΄ μμΌλ―λ‘ λ€μκ³Ό κ°μ΄ λΆκΈ°μ²λ¦¬ ν μ μλ€. | ||
* 1. μ²μμ΄ μ ν O λ§μ§λ§ X | ||
* 2. μ²μμ΄ μ ν X λ§μ§λ§ μκ΄μμ | ||
* | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 1) return nums[0]; | ||
if (nums.length === 2) return Math.max(nums[0], nums[1]); | ||
|
||
const hasFirst = Array.from({ length: nums.length }, (_, i) => | ||
i < 2 ? nums[0] : 0 | ||
); | ||
const noFirst = Array.from({ length: nums.length }, (_, i) => | ||
i === 1 ? nums[i] : 0 | ||
); | ||
for (let i = 2; i < nums.length; i++) { | ||
hasFirst[i] = Math.max(hasFirst[i - 1], hasFirst[i - 2] + nums[i]); | ||
noFirst[i] = Math.max(noFirst[i - 1], noFirst[i - 2] + nums[i]); | ||
if (i === nums.length - 1) { | ||
hasFirst[i] = Math.max(hasFirst[i - 1], hasFirst[i - 2]); | ||
} | ||
} | ||
|
||
return Math.max(hasFirst[nums.length - 1], noFirst[nums.length - 1]); | ||
}; |
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,18 @@ | ||
/** | ||
* @description | ||
* | ||
* n = length of n | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var reverseBits = function (n) { | ||
let answer = 0; | ||
let binary = n.toString(2); | ||
|
||
if (binary.length < 32) binary = "0".repeat(32 - binary.length) + binary; | ||
|
||
for (let i = binary.length - 1; i >= 0; i--) | ||
answer += Math.pow(2, i) * Number(binary[i]); | ||
|
||
return answer; | ||
}; |