-
-
Notifications
You must be signed in to change notification settings - Fork 195
[YeomChaeeun] Week 14 #1091
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
[YeomChaeeun] Week 14 #1091
Changes from all commits
Commits
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,48 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* 이진트리 레벨 순회 하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param root | ||
*/ | ||
function levelOrder(root: TreeNode | null): number[][] { | ||
if(!root) return [] | ||
let result = [] | ||
const queue: TreeNode[] = [root] | ||
|
||
while (queue.length > 0) { | ||
const levelValues: number[] = [] | ||
|
||
// 현재 레벨의 크기 (현재 큐에 있는 현재 레벨에 속한 노드의 수) 만큼 순회 | ||
for (let i = 0; i < queue.length; i++) { | ||
const node = queue.shift()! | ||
|
||
// 현재 노드의 값을 현재 레벨의 값 배열에 추가 | ||
levelValues.push(node.val) | ||
|
||
if (node.left) { | ||
queue.push(node.left); | ||
} | ||
if (node.right) { | ||
queue.push(node.right); | ||
} | ||
} | ||
|
||
result.push(levelValues) | ||
} | ||
|
||
return result; | ||
} |
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,26 @@ | ||
/** | ||
* 이진수에서 1의 개수 세기 알고리즘 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n * log n) | ||
* - 공간 복잡도: O(n) | ||
*/ | ||
function countBits(n: number): number[] { | ||
const result: number[] = []; | ||
|
||
// 비트 연산을 이용해 1의 개수를 세는 함수 - O(log n) | ||
function countOnes(num: number): number { | ||
let count = 0; | ||
while (num > 0) { | ||
num &= (num - 1); // 가장 오른쪽의 1 비트를 제거 | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
for (let i = 0; i <= n; i++) { | ||
result.push(countOnes(i)); | ||
} | ||
|
||
return result; | ||
} | ||
Comment on lines
+7
to
+25
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. @YeomChaeeun 님 안녕하세요! |
||
|
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,65 @@ | ||
|
||
/** | ||
* 시간 복잡도: O(nlogn) - sort 메소드 | ||
*/ | ||
// class MedianFinder { | ||
// arr: number[] | ||
// constructor() { | ||
// this.arr = []; | ||
// } | ||
|
||
// addNum(num: number): void { | ||
// this.arr.push(num) | ||
// this.arr.sort((a, b) => a - b); | ||
// } | ||
|
||
// findMedian(): number { | ||
// const len = this.arr.length; | ||
// const mid = Math.floor(len / 2); | ||
// if(len % 2) return this.arr[mid]; | ||
// else return (this.arr[mid - 1] + this.arr[mid]) / 2; | ||
// } | ||
// } | ||
|
||
/** | ||
* 시간 복잡도: O(log n) - 힙 연산의 시간 복잡도 | ||
* 공간 복잡도: O(n) - n개의 숫자를 저장하는 데 필요한 공간 | ||
*/ | ||
class MedianFinder { | ||
maxHeap = new MaxPriorityQueue() // 최대값을 트리의 루트에 위치시키는 힙 | ||
minHeap = new MinPriorityQueue() // 최소값을 트리의 루트에 위치시키는 힙 | ||
size: number | ||
|
||
constructor() { | ||
this.size = 0 | ||
} | ||
|
||
addNum(num: number): void { | ||
this.size++ | ||
this.maxHeap.enqueue(num) // 먼저 최대 힙에 추가 | ||
const item = this.maxHeap.dequeue() // 최대 힙의 최대값 제거 | ||
this.minHeap.enqueue(item) // 최소 힙에 추가 | ||
|
||
// 최소 힙이 크면 최대 힙으로 이동 | ||
if (this.minHeap.size() > this.maxHeap.size()+1) { | ||
const item = this.minHeap.dequeue() | ||
this.maxHeap.enqueue(item) | ||
} | ||
} | ||
|
||
findMedian(): number { | ||
if (this.size % 2 !== 0) { | ||
// 최소 힙의 최소값 | ||
return this.minHeap.front() | ||
} else { | ||
// 최대 힙의 최대값(root)과 최소 힙의 최소값(root)의 평균 | ||
return (this.maxHeap.front() + this.minHeap.front()) / 2 | ||
} | ||
} | ||
} | ||
/** | ||
* Your MedianFinder object will be instantiated and called as such: | ||
* var obj = new MedianFinder() | ||
* obj.addNum(num) | ||
* var param_2 = obj.findMedian() | ||
*/ |
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.
Queue 를 사용한 BFS로 문제를 잘 풀어주셨네요!
공간복잡도를 최적화 하기 위해 DFS 문제 풀이도 도전해보시면 재미있을것 같습니다!