Skip to content

[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 3 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions binary-tree-level-order-traversal/YeomChaeeun.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Queue 를 사용한 BFS로 문제를 잘 풀어주셨네요!
공간복잡도를 최적화 하기 위해 DFS 문제 풀이도 도전해보시면 재미있을것 같습니다!

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;
}
26 changes: 26 additions & 0 deletions counting-bits/YeomChaeeun.ts
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YeomChaeeun 님 안녕하세요!
코드의 가독성이 좋고, 비트 연산 방식이 제가 사용한 방법보다 더 효율적이어서 도움이 되었습니다. 여유가 된다면, DP로도 풀어보는 것을 추천드립니다! 이번 주도 고생하셨습니다 👍


65 changes: 65 additions & 0 deletions find-median-from-data-stream/YeomChaeeun.ts
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()
*/