-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hwi-middle] WEEK 09 solutions #2584
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 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode(int x) : val(x), next(NULL) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool hasCycle(ListNode *head) { | ||
| if (head == nullptr) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| unordered_set<ListNode*> ns; | ||
| ListNode* cur = head; | ||
| while (cur->next != nullptr) | ||
| { | ||
| if (ns.contains(cur)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| ns.insert(cur); | ||
| cur = cur->next; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| }; |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: The solution processes each element once, updating max/min products, resulting in linear time and constant space complexity. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProduct(vector<int>& nums) { | ||
| if (nums.size() == 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| int max_so_far = nums[0]; | ||
| int min_so_far = nums[0]; | ||
| int result = max_so_far; | ||
|
|
||
| for (int i = 1; i < nums.size(); i++) | ||
| { | ||
| int curr = nums[i]; | ||
| int temp_max = max(curr, max(max_so_far * curr, min_so_far * curr)); | ||
| min_so_far = min(curr, min(max_so_far * curr, min_so_far * curr)); | ||
| max_so_far = temp_max; | ||
| result = max(max_so_far, result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| }; |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: The algorithm expands and contracts the window efficiently, with each character visited at most twice, leading to linear time and space proportional to input sizes. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| class Solution { | ||
| public: | ||
| string minWindow(string s, string t) { | ||
| if (s.empty() || t.empty()) | ||
| { | ||
| return ""; | ||
| } | ||
|
|
||
| // t에서 각 문자가 등장한 횟수 | ||
| unordered_map<char, int> dictT; | ||
| for (auto& c : t) | ||
| { | ||
| dictT[c]++; | ||
| } | ||
|
|
||
| // t에 들어있는 문자의 종류 | ||
| // 즉, 윈도우 안에 있는 문자의 종류는 required개 이상이어야 함 | ||
| int required = dictT.size(); | ||
|
|
||
| int l = 0; | ||
| int r = 0; | ||
|
|
||
| // t에 있는 문자의 종류 중 현재 윈도우 내에서 필요한 만큼 채워진 문자의 수 | ||
| int formed = 0; | ||
|
|
||
| // 윈도우에 각 문자가 등장한 횟수 | ||
| unordered_map<char, int> windowCounts; | ||
|
|
||
| // 윈도우 길이, 왼쪽 인덱스, 오른쪽 인덱스 | ||
| int ans[3] = { -1, 0, 0 }; | ||
|
|
||
| while (r < s.length()) | ||
| { | ||
| // 오른쪽 포인터가 가리키는 문자를 윈도우에 추가 | ||
| char c = s[r]; | ||
| windowCounts[c]++; | ||
|
|
||
| // t에서 등장한 횟수가 같다면 formed 증가 | ||
| if (dictT.contains(c) && windowCounts[c] == dictT[c]) | ||
| { | ||
| formed++; | ||
| } | ||
|
|
||
| // 윈도우가 조건을 만족하는 동안 루프 | ||
| // 즉, 더 이상 조건을 만족하지 않을 때 까지 왼쪽 포인터를 이동하여 윈도우를 축소시켜봄 | ||
| while (l <= r && formed == required) | ||
| { | ||
| c = s[l]; | ||
|
|
||
| // 현재까지 발견한 가장 작은 윈도우 정보를 저장 | ||
| if (ans[0] == -1 || r - l + 1 < ans[0]) | ||
| { | ||
| ans[0] = r - l + 1; | ||
| ans[1] = l; | ||
| ans[2] = r; | ||
| } | ||
|
|
||
| // 왼쪽 포인터가 가리키는 문자를 윈도우에서 제거 | ||
| windowCounts[c]--; | ||
|
|
||
| // 그 결과, 문자가 t에서 등장한 횟수를 충족하지 못하면 formed 감소 | ||
| if (dictT.contains(c) && windowCounts[c] < dictT[c]) | ||
| { | ||
| formed--; | ||
| } | ||
|
|
||
| // 왼쪽 포인터 이동 | ||
| l++; | ||
| } | ||
|
|
||
| // 축소해가며 탐색이 끝났으면 오른쪽 포인터를 이동 | ||
| r++; | ||
| } | ||
|
|
||
| return ans[0] == -1 ? "" : s.substr(ans[1], ans[2] - ans[1] + 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: Each cell is enqueued at most once per ocean, resulting in linear time and space complexity relative to grid size. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) { | ||
| int r = heights.size(); | ||
| int c = heights[0].size(); | ||
|
|
||
| vector<vector<int>> result; | ||
| if (r == 0 || c == 0) return result; | ||
|
|
||
| vector<vector<bool>> pacific(r, vector<bool>(c, false)); | ||
| vector<vector<bool>> atlantic(r, vector<bool>(c, false)); | ||
|
|
||
| queue<pair<int, int>> pacific_q; | ||
| queue<pair<int, int>> atlantic_q; | ||
|
|
||
| for (int i = 0; i < r; ++i) | ||
| { | ||
| pacific_q.push({i, 0}); | ||
| pacific[i][0] = true; | ||
|
|
||
| atlantic_q.push({i, c - 1}); | ||
| atlantic[i][c - 1] = true; | ||
| } | ||
|
|
||
| for (int j = 0; j < c; ++j) | ||
| { | ||
| pacific_q.push({0, j}); | ||
| pacific[0][j] = true; | ||
|
|
||
| atlantic_q.push({r - 1, j}); | ||
| atlantic[r - 1][j] = true; | ||
| } | ||
|
|
||
| bfs(pacific_q, pacific, heights); | ||
| bfs(atlantic_q, atlantic, heights); | ||
|
|
||
| for (int i = 0; i < r; ++i) | ||
| { | ||
| for (int j = 0; j < c; ++j) | ||
| { | ||
| if (pacific[i][j] && atlantic[i][j]) | ||
| { | ||
| result.push_back({i, j}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private: | ||
| void bfs(queue<pair<int, int>>& q, vector<vector<bool>>& visited, const vector<vector<int>>& heights) | ||
| { | ||
| int r = heights.size(); | ||
| int c = heights[0].size(); | ||
|
|
||
| int dx[] = { 0, 1, 0, -1 }; | ||
| int dy[] = { 1, 0, -1, 0 }; | ||
|
|
||
| while (!q.empty()) { | ||
| int x, y; | ||
| tie(x, y) = q.front(); | ||
| q.pop(); | ||
|
|
||
| for (int dir = 0; dir < 4; ++dir) | ||
| { | ||
| int nx = x + dx[dir]; | ||
| int ny = y + dy[dir]; | ||
|
|
||
| if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue; | ||
|
|
||
| if (visited[nx][ny]) continue; | ||
|
|
||
| if (heights[nx][ny] < heights[x][y]) continue; | ||
|
|
||
| visited[nx][ny] = true; | ||
| q.push({nx, ny}); | ||
| } | ||
| } | ||
| } | ||
| }; |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: The fixed number of bits ensures constant time and space complexity, as each bit is processed once. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Solution { | ||
| public: | ||
| int getSum(int a, int b) { | ||
| int res = 0; | ||
|
|
||
| bool carry = false; | ||
| for (int i = 0; i < 32; ++i) | ||
| { | ||
| int mask = 1 << i; | ||
| bool bitA = a & mask; | ||
| bool bitB = b & mask; | ||
| bool sum = adder(bitA, bitB, carry); | ||
| if (sum) | ||
| { | ||
| res |= mask; | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| bool adder(bool a, bool b, bool& inout_carry) | ||
| { | ||
| bool axorb = a ^ b; | ||
| bool sum = axorb ^ inout_carry; | ||
| bool aandb = a & b; | ||
| inout_carry = aandb | (axorb & inout_carry); | ||
| return sum; | ||
| } | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: The algorithm traverses each node once, inserting and checking nodes in a hash set, leading to linear time and space complexity.
개선 제안: 현재 구현이 적절해 보입니다.