-
-
Notifications
You must be signed in to change notification settings - Fork 336
[hwi-middle] WEEK 13 solutions #2620
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class MedianFinder { | ||
| public: | ||
| MedianFinder() { | ||
| } | ||
|
|
||
| void addNum(int num) { | ||
| maxHeap.push(num); | ||
| minHeap.push(maxHeap.top()); | ||
| maxHeap.pop(); | ||
|
|
||
| if (maxHeap.size() < minHeap.size()) | ||
| { | ||
| maxHeap.push(minHeap.top()); | ||
| minHeap.pop(); | ||
| } | ||
| } | ||
|
|
||
| double findMedian() { | ||
| return maxHeap.size() > minHeap.size() ? maxHeap.top() : ((double) maxHeap.top() + minHeap.top()) * 0.5; | ||
| } | ||
|
|
||
| private: | ||
| priority_queue<int> maxHeap; | ||
| priority_queue<int, vector<int>, greater<int>> minHeap; | ||
| }; | ||
|
|
||
| /** | ||
| * Your MedianFinder object will be instantiated and called as such: | ||
| * MedianFinder* obj = new MedianFinder(); | ||
| * obj->addNum(num); | ||
| * double param_2 = obj->findMedian(); | ||
| */ |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 입력 구간 리스트를 한 번 순회하며, 새 구간과 겹치는 구간을 병합하고 나머지 구간을 그대로 결과에 추가한다. 전체 과정은 리스트 크기만큼 순회하므로 O(n) 시간과 공간을 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) { | ||
| int n = intervals.size(); | ||
| vector<vector<int>> res; | ||
|
|
||
| int i = 0; | ||
| while (i < n && intervals[i][1] < newInterval[0]) | ||
| { | ||
| res.push_back(intervals[i]); | ||
| i++; | ||
| } | ||
|
|
||
| while (i < n && newInterval[1] >= intervals[i][0]) | ||
| { | ||
| newInterval[0] = min(newInterval[0], intervals[i][0]); | ||
| newInterval[1] = max(newInterval[1], intervals[i][1]); | ||
| i++; | ||
| } | ||
| res.push_back(newInterval); | ||
|
|
||
| while (i < n) | ||
| { | ||
| res.push_back(intervals[i]); | ||
| i++; | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
| }; |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중위순회는 트리의 모든 노드를 방문하므로 최악의 경우 O(n) 시간이 소요된다. 재귀 호출 스택은 트리 높이만큼 차지한다. 이 방법은 트리의 구조에 따라 O(h) 공간을 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int kthSmallest(TreeNode* root, int k) { | ||
| int cur = 0; | ||
| return impl(root, k, cur); | ||
| } | ||
|
|
||
| int impl(TreeNode* root, int k, int& cur) { | ||
|
Contributor
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. method name 을 직관적으로 작성해주시면 더 좋을 것 같습니다! |
||
| if (root == nullptr) | ||
| { | ||
| return -1; | ||
| } | ||
|
|
||
| int l = impl(root->left, k, cur); | ||
| if (l != -1) | ||
| { | ||
| return l; | ||
| } | ||
|
|
||
| if (++cur == k) | ||
| { | ||
| return root->val; | ||
| } | ||
|
|
||
| int r = impl(root->right, k, cur); | ||
| if (r != -1) | ||
| { | ||
| return r; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
| }; | ||
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: BST의 성질을 이용하여 루트부터 시작해 p와 q의 값에 따라 왼쪽 또는 오른쪽으로 이동하며, 두 노드가 다른 쪽에 있으면 현재 노드가 LCA이다. 높이 h만큼 이동하므로 시간은 O(h), 공간은 상수이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
| * }; | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
| // p와 q가 root 기준 왼쪽과 오른쪽에 나뉘어 존재하면 root가 LCA | ||
| while ((root->val - p->val) * (long long)(root->val - q->val) > 0LL) | ||
| { | ||
| root = root->val > p->val ? root->left : root->right; | ||
| } | ||
|
|
||
| return root; | ||
| } | ||
| }; |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 회의를 시작 시간 기준으로 정렬하고, 인접 회의의 종료 시간과 비교하여 겹침 여부를 판단한다. 정렬이 O(n log n), 이후 검사에 O(n) 시간 소요. 공간은 정렬하는 데 필요한 공간만 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution { | ||
| public: | ||
| bool canAttendMeetings(vector<vector<int>>& intervals) { | ||
| sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) | ||
| { | ||
| return a[0] < b[0]; | ||
| }); | ||
|
|
||
| int n = intervals.size(); | ||
| for (int i = 0; i < n - 1; ++i) | ||
| { | ||
| if (intervals[i][1] > intervals[i + 1][0]) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 개의 힙(최대힙과 최소힙)을 유지하며, 새 숫자를 적절히 분배하고 균형을 맞추는 방식으로 중앙값을 O(1) 또는 O(log n) 시간에 찾는다. 삽입 시 힙 연산이 O(log n)이고, 공간은 저장된 숫자만큼 차지한다.
개선 제안: 현재 구현이 적절해 보입니다.