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
44 changes: 44 additions & 0 deletions clone-graph/hyeri0903.java
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,44 @@
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/

class Solution {
//key: original node, value: clone node
private Map<Node, Node> map = new HashMap<>();

public Node cloneGraph(Node node) {
if(node == null) {
return null;
}
//이미 존재하면 return
if(map.containsKey(node)) {
return map.get(node);
}

//현재 노드 복제
Node clone = new Node(node.val);
map.put(node, clone);

//이웃 노드들 복제해서 연결
for(Node neighbor: node.neighbors) {
clone.neighbors.add(cloneGraph(neighbor));
}
return clone;
}
}
28 changes: 28 additions & 0 deletions longest-common-subsequence/hyeri0903.java
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 테이블을 활용하는 전형적인 동적 계획법 패턴을 사용합니다. 이전 계산 결과를 저장하여 효율적으로 문제를 해결합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
/**
1.문제: 가장 긴 common subsequence length return, 없으면 0 return
2.constraints
- length min = 1, max = 1000
- text1, text2 모두 lowercase로 구성
3.풀이
- dp[i][j] = text1의 앞 i개, text2의 앞 j개를 비교했을 때 LCS 길이
- 문자가 같으면: dp[i][j] = dp[i-1][j-1] + 1
- 문자가 다르면: dp[i][j] = max(dp[i-1][j], dp[i][j-1])
*/
int t1Size = text1.length(); //row
int t2Size = text2.length(); //col
int[][] dp = new int[t1Size + 1][t2Size + 1];

for(int i = 1; i <= t1Size; i++) {
for(int j = 1; j <= t2Size; j++) {
if(text1.charAt(i-1) == text2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + 1;
} else {
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[t1Size][t2Size];
}
}
39 changes: 39 additions & 0 deletions longest-repeating-character-replacement/hyeri0903.java
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,39 @@
class Solution {
public int characterReplacement(String s, int k) {
/**
1.problem: k번 특정 문자를 골라서 바꿀 수 있다. 이때 same letter 로 이루어진 가장 긴 substring 의 길이을 구하라.
2.constraints
- s.length min = 1, max = 10^5
- k.length min = 0, max = s.length
3.solution
- sliding window: time O(n), space O(1)
*/

int[] table = new int[26];
int n = s.length();
int maxFreq = 0;
int maxLen = 0;
int left = 0;

for(int right = 0; right < n; right++) {
//1.right 확장 -> count 증가
int currentChar = s.charAt(right) - 'A';
table[currentChar]++;

//2.maxFreq update
maxFreq = Math.max(maxFreq, table[currentChar]);

//3.조건 깨지면 left 이동하면서 count--
//바꿔야하는 문자 개수가 k 보다 큰 경우
if((right - left + 1 - maxFreq) > k) {
int idx = s.charAt(left) - 'A';
table[idx]--;
left++;
}

//4.maxLen update
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
}
38 changes: 38 additions & 0 deletions palindromic-substrings/hyeri0903.java
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
  • 설명: 이 코드는 문자열의 양쪽 끝에서 시작하여 중앙으로 확장하는 방식을 사용하여 팰린드롬을 찾는 'expand around' 기법을 적용하였으며, 이는 Two Pointers 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public int countSubstrings(String s) {
/**
1.문제: s 에서 palindromoic substring 개수 return
2.constraints:
- 연속적인 문자
- s.length min = 1, max = 1000
3.풀이
- bruteforce: time:O(n^3), space:O(n)
- expand around : time(On^2), space:O(1)
*/

int n = s.length();
if(n == 1) return 1;
int count = 0;

for(int i = 0; i < n; i++) {
//odd
count += checkPalindrome(s, i, i);

//even
count += checkPalindrome(s, i, i+1);
}
return count;

}

private int checkPalindrome(String s, int left, int right) {
int count = 0;
while(left >= 0 && right < s.length() && s.charAt(right) == s.charAt(left)) {
count += 1;
left -= 1;
right += 1;
}
return count;
}

}
11 changes: 11 additions & 0 deletions reverse-bits/hyeri0903.java
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,11 @@
class Solution {
public int reverseBits(int n) {
int result = 0;
for(int i = 0; i < 32; i++) {
result <<= 1; //왼쪽 이동
result |= (n & 1); //마지막 비트 붙이기
n >>= 1; // 오른쪽 이동
}
return result;
}
}
Loading