Skip to content

[crumbs22] Week 14 Solutions #1644

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 1 commit into from
Jul 5, 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
32 changes: 32 additions & 0 deletions binary-tree-level-order-traversal/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
queue를 활용해서 자식 노드의 수를 세고, 그만큼 반복문을 돌면서
각 노드의 값을 저장하는 방식으로 깊이순 트리 순회를 구현
트리의 모든 노드를 한 번씩 방문하므로 O(n)의 시간복잡도,
트리의 모든 노드를 저장하므로 O(n)의 공간 복잡도를 가진다.
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> q;
vector<vector<int>> ans;
if (!root)
return ans;

q.push(root);
while (!q.empty()) {
vector<int> tmp;
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* child = q.front();
q.pop();
tmp.push_back(child->val);
if (child->left)
q.push(child->left);
if (child->right)
q.push(child->right);
}
ans.push_back(tmp);
}
return ans;
}
};
15 changes: 15 additions & 0 deletions counting-bits/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
n + 1번 반복하며 dp배열에 저장하기 때문에 시간복잡도 O(n), 공간복잡도 O(n)만큼 소요
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> dp(n + 1, 0); // 0부터 n까지이므로 n + 1개 필요, dp[0] = 0

// dp배열을 활용, 최상위비트 + (마지막비트&1)를 dp에 저장
for (int num = 1; num < n + 1; num++) {
dp[num] = dp[num >> 1] + (num & 1);
Copy link
Member

Choose a reason for hiding this comment

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

비트 연산으로 풀이 진행하셨군요 👍

}
return dp;
}
};
32 changes: 32 additions & 0 deletions house-robber-ii/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int rob(vector<int>& nums) {

if (nums.size() == 1)
return (nums[0]);

// 첫번째 집 ~ 마지막 - 1번째 집까지
int prev = 0;
int curr = nums[0];

for (int i = 1; i + 1 < nums.size(); i++) {
int tmp = curr;
// 바로 전 집을 터는 게 나은지, 전전번째 집과 현재(nums[i])집을 터는 게 나은지 판별
curr = max(curr, prev + nums[i]);
prev = tmp; // 전전번째 집의 값을 갱신
}
int fstrob = curr;

// 두번째 집 ~ 마지막 집까지
prev = 0;
curr = nums[1];
for (int i = 2; i < nums.size(); i++) {
int tmp = curr;
curr = max(curr, prev + nums[i]);
prev = tmp;
}
int scdrob = curr;

return max(fstrob, scdrob);
}
};
25 changes: 25 additions & 0 deletions meeting-rooms-ii/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
정렬에 O(nlogn), 힙 삽입 및 삭제에 O(logn) 소요되므로 전체 시간 복잡도는 O(nlogn)
최대 n개의 회의가 있을 수 있으므로 공간 복잡도는 O(n)
*/
class Solution {
public:
int minMeetingRooms(vector<Interval> &intervals) {
if (intervals.empty())
return 0;

sort(intervals.begin(), intervals.end()); // 시작 시간 기준 정렬
priority_queue<int, vector<int>, greater<int>> minHeap; // 최소힙에 회의 종료 시간 저장

minHeap.push(intervals[0][1]);
Copy link
Member

Choose a reason for hiding this comment

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

리뷰과정에서 cpp코드가 모르는부분이 있어 gpt로 찾아보니
Interval타입은 직접인덱싱이 불가하다던데 혹시 이부분 설명해주실수 있으신가요?

for (int i = 1; i < intervals.size(); i++) {
int start = intervals[i][0];
int end = intervals[i][1];

if (start >= minHeap.top()) // 재사용 가능할 때 pop
minHeap.pop();
minheap.push(end); // 새로운 회의 종료시간 힙에 추가
}
return minHeap.size();
}
};