Skip to content
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 find-median-from-data-stream/hwi-middle.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Heap / Priority Queue
  • 설명: 이 코드는 두 개의 힙을 이용해 데이터 스트림의 중앙값을 효율적으로 찾는 방법을 구현했으며, 힙 자료구조를 활용하는 패턴입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(log n)
Space O(n)

피드백: 두 개의 힙(최대힙과 최소힙)을 유지하며, 새 숫자를 적절히 분배하고 균형을 맞추는 방식으로 중앙값을 O(1) 또는 O(log n) 시간에 찾는다. 삽입 시 힙 연산이 O(log n)이고, 공간은 저장된 숫자만큼 차지한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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();
*/
30 changes: 30 additions & 0 deletions insert-interval/hwi-middle.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 정렬된 구간 배열과 새 구간을 병합하는 과정에서 두 포인터를 활용하여 효율적으로 구간을 처리하는 패턴입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 입력 구간 리스트를 한 번 순회하며, 새 구간과 겹치는 구간을 병합하고 나머지 구간을 그대로 결과에 추가한다. 전체 과정은 리스트 크기만큼 순회하므로 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;
}
};
44 changes: 44 additions & 0 deletions kth-smallest-element-in-a-bst/hwi-middle.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 이진 탐색 트리의 중위 순회를 재귀적으로 수행하여 k번째 작은 값을 찾는 방식으로, 깊이 우선 탐색(DFS) 패턴을 사용합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 중위순회는 트리의 모든 노드를 방문하므로 최악의 경우 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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
};
22 changes: 22 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/hwi-middle.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 이 코드는 이진 탐색 트리의 특성을 이용하여 두 노드의 공통 조상을 찾는 방식으로, 값 비교를 통해 탐색 범위를 좁혀가는 이진 탐색 패턴을 사용합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(h)
Space O(1)

피드백: 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;
}
};
20 changes: 20 additions & 0 deletions meeting-rooms/hwi-middle.cpp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Sorting
  • 설명: 이 코드는 회의 시간 간격을 정렬한 후 겹치는지 검사하는 방식으로, 정렬을 활용하는 패턴에 속합니다. 정렬 후 순차적 비교로 문제를 해결하는 대표적인 방법입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(1)

피드백: 회의를 시작 시간 기준으로 정렬하고, 인접 회의의 종료 시간과 비교하여 겹침 여부를 판단한다. 정렬이 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;
}
};
Loading