-
-
Notifications
You must be signed in to change notification settings - Fork 193
[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
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,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; | ||
} | ||
}; |
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,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); | ||
} | ||
return dp; | ||
} | ||
}; |
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,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); | ||
} | ||
}; |
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,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]); | ||
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. 리뷰과정에서 cpp코드가 모르는부분이 있어 gpt로 찾아보니 |
||
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(); | ||
} | ||
}; |
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.
비트 연산으로 풀이 진행하셨군요 👍