-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hwi-middle] WEEK 10 solutions #2596
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,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; | ||
| } | ||
| }; |
|
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,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; | ||
| } | ||
| }; |
|
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,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; | ||
| } | ||
| }; |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 노드를 우선순위 큐에 넣고 꺼내는 과정에서 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; | ||
| } | ||
| }; |
|
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(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); | ||
| } | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 그래프를 인접 리스트로 표현하고 DFS를 통해 방문 상태를 체크하여 사이클을 검증하는 방식으로, 각 노드와 간선을 한 번씩 탐색하므로 시간 복잡도는 O(V + E)입니다. 공간은 그래프 저장과 재귀 호출 스택, 방문 상태 배열을 고려하여 O(V + E)입니다.
개선 제안: 현재 구현이 적절해 보입니다.