-
-
Notifications
You must be signed in to change notification settings - Fork 361
[JeonJe] WEEK 08 Solutions #2812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(V + E) | ||
| // SC: O(V) | ||
| class Solution { | ||
| public Node cloneGraph(Node node) { | ||
| return deepCopy(node, new HashMap<>()); | ||
| } | ||
|
|
||
| private Node deepCopy(Node node, Map<Node, Node> cloned) { | ||
| if (node == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (cloned.containsKey(node)) { | ||
| return cloned.get(node); | ||
| } | ||
|
|
||
| Node clonedNode = new Node(node.val); | ||
| cloned.put(node, clonedNode); | ||
|
|
||
| for (Node neighbor : node.neighbors) { | ||
| clonedNode.neighbors.add(deepCopy(neighbor, cloned)); | ||
| } | ||
|
|
||
| return clonedNode; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석longest-common-subsequence/JeonJe.java// TC: O(m * n)
// SC: O(m * n)
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
for (int i = text1.length() - 1; i >= 0; i--) {
for (int j = text2.length() - 1; j >= 0; j--) {
dp[i][j] = text1.charAt(i) == text2.charAt(j) ?
1 + dp[i + 1][j + 1] :
Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
return dp[0][0];
}
}
📊 시간/공간 복잡도 분석
피드백: 두 문자열의 남은 부분 문제를 좌상단부터 채우는 대신 역방향으로 채워서 최종 dp[0][0]을 구한다. 메모리 사용은 두 문자열의 길이의 곱에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공간에서 좀 더 최적화가 가능합니다. 참고하세요
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dp[i][j]가 i행과 i+1행만 참조하기 때문에, 한 배열로 제자리 덮어쓰면서 대각선 값만 변수로 넘기면 O(min(m, n))로 가능하겠네요. 감사합니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(m * n) | ||
| class Solution { | ||
|
|
||
| public int longestCommonSubsequence(String text1, String text2) { | ||
| int[][] dp = new int[text1.length() + 1][text2.length() + 1]; | ||
|
|
||
| for (int i = text1.length() - 1; i >= 0; i--) { | ||
| for (int j = text2.length() - 1; j >= 0; j--) { | ||
|
|
||
| dp[i][j] = text1.charAt(i) == text2.charAt(j) ? | ||
| 1 + dp[i + 1][j + 1] : | ||
| Math.max(dp[i + 1][j], dp[i][j + 1]); | ||
|
|
||
| } | ||
| } | ||
| return dp[0][0]; | ||
| } | ||
|
|
||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석longest-repeating-character-replacement/JeonJe.javaimport java.util.*;
// TC: O(n)
// SC: O(1)
class Solution {
public int characterReplacement(String s, int k) {
int[] counts = new int[26];
int left = 0;
for (int right = 0; right < s.length(); right++) {
counts[toAlphabetIndex(s.charAt(right))]++;
int windowLength = right - left + 1;
if (windowLength - countMostFrequent(counts) > k) {
counts[toAlphabetIndex(s.charAt(left))]--;
left++;
}
}
return s.length() - left;
}
private int toAlphabetIndex(char c) {
return c - 'A';
}
private int countMostFrequent(int[] counts) {
int max = 0;
for (int count : counts) {
max = Math.max(max, count);
}
return max;
}
}
📊 시간/공간 복잡도 분석
피드백: 두 포인터와 알파벳 카운트 배열을 이용해 윈도우 내 최댓 등장 문자 수를 유지한다. 각 단계에서 상한 조건을 확인하고 필요 시 왼쪽 포인터를 한 칸 이동한다. 개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석longest-repeating-character-replacement/JeonJe.javaimport java.util.*;
// TC: O(n)
// SC: O(1)
class Solution {
public int characterReplacement(String s, int k) {
int[] counts = new int[26];
int left = 0;
for (int right = 0; right < s.length(); right++) {
counts[toAlphabetIndex(s.charAt(right))]++;
int windowLength = right - left + 1;
int mostFreq = Arrays.stream(counts).max().getAsInt();
//바꿀 대상이 k 횟수보다 크면, left을 옮김
if (windowLength - mostFreq > k) {
counts[toAlphabetIndex(s.charAt(left))]--;
left++;
}
}
return s.length() - left;
}
private int toAlphabetIndex(char c) {
return c - 'A';
}
}
📊 시간/공간 복잡도 분석
피드백: 문자 빈도 배열을 유지하고 윈도우의 크기를 확장하며 필요한 경우 왼쪽 포인터를 이동시킨다. 최댓값 계산은 상수 배열에서의 최대를 매 반복에서 갱신해도 된다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(1) | ||
| class Solution { | ||
| public int characterReplacement(String s, int k) { | ||
| int[] counts = new int[26]; | ||
| int left = 0; | ||
|
|
||
| for (int right = 0; right < s.length(); right++) { | ||
| counts[toAlphabetIndex(s.charAt(right))]++; | ||
|
|
||
| int windowLength = right - left + 1; | ||
| int mostFreq = Arrays.stream(counts).max().getAsInt(); | ||
| //바꿀 대상이 k 횟수보다 크면, left을 옮김 | ||
| if (windowLength - mostFreq > k) { | ||
| counts[toAlphabetIndex(s.charAt(left))]--; | ||
| left++; | ||
| } | ||
| } | ||
|
|
||
| return s.length() - left; | ||
| } | ||
|
|
||
| private int toAlphabetIndex(char c) { | ||
| return c - 'A'; | ||
| } | ||
|
|
||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석reverse-bits/JeonJe.javaimport java.util.*;
// TC: O(1)
// SC: O(1)
class Solution {
public int reverseBits(int n) {
int answer = 0;
for (int i = 0; i < 32; i++) {
int bitFlag = (n >> i) & 1;
answer += (bitFlag << (31 - i));
}
return answer;
}
}
📊 시간/공간 복잡도 분석
피드백: 고정된 32비트 순회를 통해 비트를 뒤집으므로 시간 복잡도는 상수 시간에 가깝고 공간도 상수이다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(1) | ||
| // SC: O(1) | ||
| class Solution { | ||
| public int reverseBits(int n) { | ||
| int answer = 0; | ||
| for (int i = 0; i < 32; i++) { | ||
| int bitFlag = (n >> i) & 1; | ||
| answer += (bitFlag << (31 - i)); | ||
| } | ||
| return answer; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
clone-graph/JeonJe.java
📊 시간/공간 복잡도 분석
피드백: 깊은 복사를 위해 맵에 원래 노드와 복제 노드를 매핑하고, 각 노드의 이웃을 재귀적으로 복제한다. 이미 복제된 노드는 재방문 시 중복 생성을 방지한다.
개선 제안: 현재 구현이 적절해 보입니다.