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 linked-list-cycle/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 순회하면서 노드를 저장하는 해시셋을 사용하여 순환 여부를 판단하는 방식으로, 해시 맵 또는 해시 세트 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

피드백: The algorithm traverses each node once, inserting and checking nodes in a hash set, leading to linear time and space complexity.

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

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

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;
}
};
24 changes: 24 additions & 0 deletions maximum-product-subarray/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 연속된 부분 배열의 최대 곱을 찾기 위해 이전 상태를 기반으로 최적의 값을 갱신하는 방식으로, 동적 프로그래밍 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

피드백: 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;
}
};
77 changes: 77 additions & 0 deletions minimum-window-substring/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window, Hash Map / Hash Set
  • 설명: 이 코드는 슬라이딩 윈도우 기법으로 문자열 범위를 조절하며 조건을 만족하는 최소 구간을 찾는다. 해시 맵을 활용해 문자 등장 횟수와 조건 체크를 효율적으로 수행한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(
Space O(

피드백: 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);
}
};
81 changes: 81 additions & 0 deletions pacific-atlantic-water-flow/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: BFS
  • 설명: 이 코드는 두 개의 큐를 이용한 너비 우선 탐색(BFS)을 통해 물이 흐를 수 있는 위치를 찾는 방식으로 문제를 해결합니다. BFS는 최단 거리 또는 레벨 탐색에 적합하며, 여기서는 인접한 위치를 차례로 탐색하는 데 사용됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(r * c)
Space O(r * c)

피드백: 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});
}
}
}
};
30 changes: 30 additions & 0 deletions sum-of-two-integers/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 이 코드는 비트 연산을 활용하여 두 정수의 합을 계산하는 방식으로, 비트 조작과 캐리 처리를 통해 덧셈을 수행하는 패턴입니다.

📊 시간/공간 복잡도 분석

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

피드백: 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;
}
};
Loading