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
22 changes: 22 additions & 0 deletions longest-substring-without-repeating-characters/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
  • 설명: 이 코드는 연속된 부분 문자열을 찾기 위해 윈도우 범위를 조절하며 탐색하는 슬라이딩 윈도우 패턴을 사용합니다. 중복 문자 제거와 최장 길이 계산에 적합한 기법입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.size();
unordered_map<char, int> m; // 현재 윈도우 내에 있는 문자 - 인덱스
int maxLen = 0;
int l = 0;
for (int r = 0; r < n; ++r)
{
if (m.contains(s[r])) // 중복된 문자를 찾은 경우
{
l = max(m[s[r]] + 1, l); // 해당 문자가 l보다 뒤에 있다면 l을 거기로 옮김
}

m[s[r]] = r; // s[r]의 위치 업데이트

maxLen = max(maxLen, r - l + 1);
}

return maxLen;
}
};
49 changes: 49 additions & 0 deletions number-of-islands/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(너비 우선 탐색)를 활용하여 섬의 연결된 영역을 탐색하고 방문 여부를 체크합니다. 큐를 이용한 탐색 방식이 핵심입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
// BFS 기반 풀이
int r = grid.size();
int c = grid[0].size();

int cnt = 0;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
if (grid[i][j] == '0')
{
continue;
}

cnt++;
queue<pair<int, int>> q;
q.push({i, j});
grid[i][j] = '0'; // 방문 여부를 저장하기 위해 grid를 직접 수정

while (!q.empty())
{
int x, y;
tie(x, y) = q.front();
q.pop();

int dx[] = { 0, 1, 0, -1 };
int dy[] = { 1, 0, -1, 0 };

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 (grid[nx][ny] == '0') continue;

grid[nx][ny] = '0';
q.push({nx, ny});
}
}
}
}

return cnt;
}
};
28 changes: 28 additions & 0 deletions reverse-linked-list/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 두 포인터(prev, cur)를 활용하여 링크드 리스트를 역순으로 뒤집는 과정에서 포인터를 이동하며 노드들을 재연결하는 방식으로 동작합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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 {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* cur = head;

// next를 이전 노드로 설정해나가며 루프
while (cur != nullptr)
{
ListNode* tmp = cur->next;
cur->next = prev;
prev = cur;
cur = tmp;
}

return prev;
}
};
108 changes: 108 additions & 0 deletions set-matrix-zeroes/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 두 번째 솔루션은 행과 열의 상태를 첫 번째 요소를 활용하여 표시하는데, 이는 해시 맵 또는 배열을 이용한 마킹 기법과 유사하며, 인덱스 활용이 특징입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 풀이 1
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<pair<int, int>> v;
v.reserve(m * n);

// 0인 칸을 별도의 공간에 마킹
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (matrix[i][j] == 0)
{
v.push_back({i, j});
}
}
}

// 마킹이 끝난 후 해당 칸들을 확인하며 행과 열을 채움
for (auto coord : v)
{
int x = coord.first;
int y = coord.second;

//
for (int i = 0; i < n; ++i)
{
matrix[x][i] = 0;
}

//
for (int i = 0; i < m; ++i)
{
matrix[i][y] = 0;
}
}
}
};

// 풀이 2
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int r = matrix.size();
int c = matrix[0].size();

// 핵심 아이디어
// - 각 행과 열에는 반드시 첫 번째 요소는 존재한다.
// - 이를 활용하여 각 행과 열의 첫 번째 요소를 마킹용으로 사용한다.
// - matrix[i][0]: i번째 행 전체를 0으로 만들어야 한다는 의미
// - matrix[0][j]: j번째 열 전체를 0으로 만들어야 한다는 의미

// 엣지 케이스
// - matrix[0][0]: 0번째 행인지, 0번째 열인지 구분할 수 없음

// 해결 방법
// - matrix[0][0]은 0번째 행을 의미하도록 약속
// - 0번째 열은 별도의 플래그를 둠

bool isFirstColZero = false;
for (int i = 0; i < r; ++i)
{
if (matrix[i][0] == 0)
{
isFirstColZero = true;
}

for (int j = 1; j < c; ++j)
{
if (matrix[i][j] == 0)
{
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}

for (int i = 1; i < r; ++i)
{
for (int j = 1; j < c; ++j)
{
if (matrix[i][0] == 0 || matrix[0][j] == 0)
{
matrix[i][j] = 0;
}
}
}

if (matrix[0][0] == 0)
{
for (int j = 0; j < c; j++)
{
matrix[0][j] = 0;
}
}

if (isFirstColZero)
{
for (int i = 0; i < r; i++)
{
matrix[i][0] = 0;
}
}
}
};
20 changes: 20 additions & 0 deletions unique-paths/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Math
  • 설명: 이 코드는 조합 계산을 통해 경로의 수를 구하는 수학적 접근을 사용하며, 특정 알고리즘 패턴에 속하지 않습니다. 일반적인 패턴 목록에는 포함되지 않는 수학적 방법입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int uniquePaths(int m, int n) {
// 결과 구하기: C(m + n - 2, m - 1)

int k = min(m - 1, n - 1); // C(n, k) = C(n, n - k)
return combination(m + n - 2, k);
}

int combination(int n, int k)
{
long long res = 1;
for (int i = 1; i <= k; ++i)
{
res = res * (n - i + 1) / i;
}

return res;
}
};
Loading