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
47 changes: 47 additions & 0 deletions clone-graph/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.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 그래프의 노드를 재귀적으로 탐색하며 복제하는 DFS 패턴을 사용합니다. 방문한 노드를 저장하여 중복 방문을 방지합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/

class Solution {
public:
Node* cloneGraph(Node* node) {
if (node == nullptr)
{
return node;
}

if (nodeMap.contains(node))
{
return nodeMap[node];
}

Node* copied = new Node(node->val);
nodeMap[node] = copied;
for (auto n : node->neighbors)
{
copied->neighbors.push_back(cloneGraph(n));
}

return copied;
}

private:
unordered_map<Node*, Node*> nodeMap;
};
26 changes: 26 additions & 0 deletions longest-common-subsequence/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
  • 설명: 이 코드는 최장 공통 부분 수열 문제를 해결하기 위해 DP 테이블을 1차원 배열로 최적화하여 사용합니다. 두 문자열의 부분 수열을 비교하며 최장 길이를 계산하는 전형적인 DP 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<int> d(text1.length(), 0);
int ans = 0;

for (auto c : text2)
{
int cur = 0;
for (int i = 0; i < d.size(); i++)
{
if (cur < d[i])
{
cur = d[i];
}
else if (c == text1[i])
{
d[i] = cur + 1;
ans = max(ans, cur + 1);
}
}
}

return ans;
}
};
25 changes: 25 additions & 0 deletions longest-repeating-character-replacement/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
  • 설명: 이 코드는 고정 크기의 윈도우를 이동시키며 문자열을 탐색하는 슬라이딩 윈도우 패턴을 사용합니다. 또한 문자 빈도 수를 세기 위해 해시 맵을 활용하여 효율적인 계산을 수행합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int characterReplacement(string s, int k) {
unordered_map<char, int> freq;
int res = 0;
int i = 0;
int maxFreq = 0;

for (int j = 0; j < s.size(); j++)
{
freq[s[j]]++;
maxFreq = max(maxFreq, freq[s[j]]);

while ((j - i + 1) - maxFreq > k)
{
freq[s[i]]--;
i++;
}

res = max(res, j - i + 1);
}

return res;
}
};
35 changes: 35 additions & 0 deletions palindromic-substrings/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
  • 설명: 이 코드는 문자열의 부분 문자열이 회문인지 여부를 저장하는 2차원 DP 테이블을 활용하여, 중복 계산을 피하고 효율적으로 회문 개수를 세는 방식입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int countSubstrings(string s) {
int n = s.size();
if (n == 0)
{
return 0;
}

vector<vector<int>> dp(n, vector<int>(n));

int ans = n;
for (int i = 0; i < n; ++i)
{
dp[i][i] = true;
}

for (int i = 0; i < n - 1; ++i)
{
dp[i][i + 1] = (s[i] == s[i + 1]);
ans += dp[i][i + 1];
}

for (int l = 3; l <= n; ++l)
{
for (int i = 0, j = i + l - 1; j < n; ++i, ++j)
{
dp[i][j] = dp[i + 1][j - 1] && (s[i] == s[j]);
ans += dp[i][j];
}
}

return ans;
}
};
17 changes: 17 additions & 0 deletions reverse-bits/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
  • 설명: 이 코드는 비트 연산을 활용하여 정수의 비트 순서를 뒤집는 문제로, 비트 조작 기법을 사용하는 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int reverseBits(int n) {
int r = 0;

for (int i = 0; i <= 31; ++i)
{
int pot = 1 << i;
if ((n & pot) != 0)
{
r |= (1 << (31 - i));
}
}

return r;
}
};
Loading