From 17fedf211d8833fd2514390d0d169406e4da4f9c Mon Sep 17 00:00:00 2001 From: Ju Hwijung Date: Sun, 3 May 2026 23:39:10 +0900 Subject: [PATCH] =?UTF-8?q?9=EC=A3=BC=EC=B0=A8=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4=205=EA=B0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linked-list-cycle/hwi-middle.cpp | 32 +++++++++ maximum-product-subarray/hwi-middle.cpp | 24 +++++++ minimum-window-substring/hwi-middle.cpp | 77 ++++++++++++++++++++ pacific-atlantic-water-flow/hwi-middle.cpp | 81 ++++++++++++++++++++++ sum-of-two-integers/hwi-middle.cpp | 30 ++++++++ 5 files changed, 244 insertions(+) create mode 100644 linked-list-cycle/hwi-middle.cpp create mode 100644 maximum-product-subarray/hwi-middle.cpp create mode 100644 minimum-window-substring/hwi-middle.cpp create mode 100644 pacific-atlantic-water-flow/hwi-middle.cpp create mode 100644 sum-of-two-integers/hwi-middle.cpp diff --git a/linked-list-cycle/hwi-middle.cpp b/linked-list-cycle/hwi-middle.cpp new file mode 100644 index 0000000000..2d21e79fb8 --- /dev/null +++ b/linked-list-cycle/hwi-middle.cpp @@ -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 ns; + ListNode* cur = head; + while (cur->next != nullptr) + { + if (ns.contains(cur)) + { + return true; + } + + ns.insert(cur); + cur = cur->next; + } + + return false; + } +}; diff --git a/maximum-product-subarray/hwi-middle.cpp b/maximum-product-subarray/hwi-middle.cpp new file mode 100644 index 0000000000..4855084909 --- /dev/null +++ b/maximum-product-subarray/hwi-middle.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int maxProduct(vector& 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; + } +}; diff --git a/minimum-window-substring/hwi-middle.cpp b/minimum-window-substring/hwi-middle.cpp new file mode 100644 index 0000000000..a1c37e7ac9 --- /dev/null +++ b/minimum-window-substring/hwi-middle.cpp @@ -0,0 +1,77 @@ +class Solution { +public: + string minWindow(string s, string t) { + if (s.empty() || t.empty()) + { + return ""; + } + + // t에서 각 문자가 등장한 횟수 + unordered_map 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 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); + } +}; diff --git a/pacific-atlantic-water-flow/hwi-middle.cpp b/pacific-atlantic-water-flow/hwi-middle.cpp new file mode 100644 index 0000000000..67c35f5360 --- /dev/null +++ b/pacific-atlantic-water-flow/hwi-middle.cpp @@ -0,0 +1,81 @@ +class Solution { +public: + vector> pacificAtlantic(vector>& heights) { + int r = heights.size(); + int c = heights[0].size(); + + vector> result; + if (r == 0 || c == 0) return result; + + vector> pacific(r, vector(c, false)); + vector> atlantic(r, vector(c, false)); + + queue> pacific_q; + queue> 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>& q, vector>& visited, const vector>& 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}); + } + } + } +}; diff --git a/sum-of-two-integers/hwi-middle.cpp b/sum-of-two-integers/hwi-middle.cpp new file mode 100644 index 0000000000..72c83fa27f --- /dev/null +++ b/sum-of-two-integers/hwi-middle.cpp @@ -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; + } +};