-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] Week 14 #1089
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
Merged
Merged
[jdy8739] Week 14 #1089
Changes from all commits
Commits
Show all changes
4 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 hidden or 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,42 @@ | ||
/** | ||
* 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 {number[][]} | ||
*/ | ||
var levelOrder = function (root) { | ||
if (!root) { | ||
return []; | ||
} | ||
|
||
const queue = [root]; | ||
const answer = []; | ||
|
||
while (queue.length > 0) { | ||
const values = []; | ||
|
||
const currentQueueLen = queue.length; | ||
|
||
for (let i = 0; i < currentQueueLen; i++) { | ||
const head = queue.shift(); | ||
|
||
values.push(head.val); | ||
|
||
head.left && queue.push(head.left); | ||
head.right && queue.push(head.right); | ||
} | ||
|
||
answer.push(values); | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 모든 노드를 한번씩 너비우선탐색으로 방문하므로 | ||
// 공간복잡도 O(n) -> 큐에 모든 노드의 값을 저장하므로 |
This file contains hidden or 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,50 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function (n) { | ||
const arr = [0]; | ||
|
||
for (let i = 1; i <= n; i++) { | ||
const num = binary(i); | ||
arr.push(num); | ||
} | ||
|
||
return arr; | ||
}; | ||
|
||
/** 성능이 느리지만 간결한 함수 */ | ||
function binary(n) { | ||
return n.toString(2).split('').filter((el) => el === '1').length; | ||
} | ||
|
||
// 시간복잡도 O(n2) -> n을 이진수문자열로 변환하고 이를 벼열화하여 1인 원소만 필터링하고 그 결과의 길이를 구한다. | ||
// 여기서 filter를 사용하여 배열을 한 번 순회하기 때문에 for문과 중첩되어 2중 루프의 시간복잡도를 가짐 | ||
|
||
/** 성능이 빠르지만 복잡한 함수 */ | ||
function binary(n) { | ||
let num = 1; | ||
let count = 0; | ||
|
||
while (num * 2 <= n) { | ||
num = num * 2; | ||
} | ||
|
||
while (0 <= n) { | ||
if (num <= n) { | ||
n = n - num; | ||
count++; | ||
} | ||
|
||
if (num === 1) { | ||
break; | ||
} | ||
|
||
num = num / 2; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
// 시간복잡도 O(n2) -> for문 안에 while문이 돌면서 i가 이진수로 변환될 경우 1이 몇개인지 반환하기 때문에 2중 루프의 시간복잡도를 가짐 | ||
// 공간복잡도 O(n) -> for문을 돌면서 arr에 i가 이진수로 변환될 경우 1이 몇 개인지 원소로 추가함 |
This file contains hidden or 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,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 1) { | ||
return nums[0]; | ||
} | ||
|
||
const dp1 = [0, nums[0]]; | ||
const dp2 = [0, 0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
const prevIndex = i - 1; | ||
|
||
const dp1Max = Math.max(dp1[prevIndex] + nums[i], dp1[i]); | ||
dp1.push(dp1Max); | ||
|
||
const dp2Max = Math.max(dp2[prevIndex] + nums[i], dp2[i]); | ||
dp2.push(dp2Max); | ||
} | ||
|
||
return Math.max(dp1[dp1.length - 2], dp2[dp2.length - 1]) | ||
}; | ||
|
||
// 시간복잡도 O(n) -> nums의 길이만큼 for문에서 최대값을 dp계산 | ||
// 공간복잡도 O(n) -> nums의 길이만큼 dp배열에 원소가 추가됨 |
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.
BFS 를 사용해서 잘 풀어주셧네요!
공간복잡도를 좀 더 효율적으로 사용하기 위해 O(h), h 는 재귀스택 만큼의 공간
으로 문제를 해결할 수도 있습니다 :)