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
64 changes: 64 additions & 0 deletions course-schedule/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
  • 설명: 이 코드는 그래프의 순환 여부를 DFS로 탐색하여 사이클을 감지하는 방식으로 문제를 해결합니다. 재귀적 탐색을 통해 각 노드의 상태를 체크하며, 그래프 탐색 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(V + E)
Space O(V + E)

피드백: 그래프를 인접 리스트로 표현하고 DFS를 통해 방문 상태를 체크하여 사이클을 검증하는 방식으로, 각 노드와 간선을 한 번씩 탐색하므로 시간 복잡도는 O(V + E)입니다. 공간은 그래프 저장과 재귀 호출 스택, 방문 상태 배열을 고려하여 O(V + E)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class Solution {
private:
enum class eVisitStatus
{
Unvisited,
Visiting,
Visited
};

public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses);

for (auto& p : prerequisites)
{
int course = p[0];
int prerequisite = p[1];

graph[course].push_back(prerequisite);
}

vector<eVisitStatus> status(numCourses, eVisitStatus::Unvisited);

for (int i = 0; i < numCourses; ++i)
{
if (status[i] == eVisitStatus::Unvisited)
{
if (!dfs(i, graph, status))
{
return false;
}
}
}

return true;
}

private:
bool dfs(int course, vector<vector<int>>& graph, vector<eVisitStatus>& status)
{
if (status[course] == eVisitStatus::Visiting)
{
return false;
}

if (status[course] == eVisitStatus::Visited)
{
return true;
}

status[course] = eVisitStatus::Visiting;

for (int prerequisite : graph[course])
{
if (!dfs(prerequisite, graph, status))
{
return false;
}
}

status[course] = eVisitStatus::Visited;
return true;
}
};
26 changes: 26 additions & 0 deletions invert-binary-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.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 재귀 호출을 통해 트리의 왼쪽과 오른쪽 자식을 탐색하며 노드의 자식을 교환하는 방식으로 동작하여 DFS 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

피드백: 트리의 모든 노드를 한 번씩 방문하며 좌우 자식을 교환하므로 시간 복잡도는 O(n)입니다. 공간은 재귀 호출 스택의 깊이(트리 높이)에 따라 O(h)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr || (root->left == nullptr && root->right == nullptr))
{
return root;
}

invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);

return root;
}
};
18 changes: 18 additions & 0 deletions jump-game/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy
  • 설명: 이 코드는 매 단계에서 최적의 선택을 하여 최대 이동 범위를 갱신하는 그리디 알고리즘으로 문제를 해결합니다.

📊 시간/공간 복잡도 분석

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

피드백: 배열을 한 번 순회하며 최대 도달 거리를 갱신하므로 시간 복잡도는 O(n). 추가 공간은 상수입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool canJump(vector<int>& nums) {
int maxDist = 0;

for (int i = 0; i < nums.size(); ++i)
{
if (maxDist < i)
{
return false;
}

maxDist = max(maxDist, i + nums[i]);
}

return true;
}
};
58 changes: 58 additions & 0 deletions merge-k-sorted-lists/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(N log N)
Space O(N)

피드백: 모든 노드를 우선순위 큐에 넣고 꺼내는 과정에서 N개의 노드에 대해 힙 연산이 수행되므로 시간 복잡도는 O(N log N). 공간은 우선순위 큐에 저장되는 노드 수에 따라 O(N)입니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
private:
struct cmp
{
bool operator() (ListNode* a, ListNode* b)
{
return a->val > b->val;
}
};

public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty())
{
return nullptr;
}

priority_queue<ListNode*, vector<ListNode*>, cmp> pq;

for (ListNode* list: lists)
{
while (list != nullptr)
{
pq.push(list);
list = list->next;
}
}

if (pq.empty())
{
return nullptr;
}

ListNode* res = pq.top();
ListNode* prev = res;
while (!pq.empty())
{
ListNode* n = pq.top();
pq.pop();
prev->next = n;
prev = n;
}

prev->next = nullptr;

return res;
}
};
38 changes: 38 additions & 0 deletions search-in-rotated-sorted-array/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(log n)
Space O(1)

피드백: 이진 탐색을 재귀로 수행하며 배열의 회전 여부를 판단하여 검색 범위를 좁히므로 시간 복잡도는 O(log n). 공간은 재귀 호출 스택에 따라 O(log n) 또는 O(1) (반복문 사용 시).

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
return searchImpl(nums, 0, nums.size() - 1, target);
}

private:
int searchImpl(vector<int>& nums, int start, int end, int target)
{
if (start > end)
{
return -1;
}

int mid = (start + end) / 2;
if (nums[mid] == target)
{
return mid;
}

if (nums[start] <= nums[mid])
{
if (nums[start] <= target && nums[mid] >= target)
{
return searchImpl(nums, start, mid - 1, target);
}

return searchImpl(nums, mid + 1, end, target);
}

if (nums[mid] <= target && nums[end] >= target)
{
return searchImpl(nums, mid + 1, end, target);
}

return searchImpl(nums, start, mid - 1, target);
}
};
Loading