-
-
Notifications
You must be signed in to change notification settings - Fork 361
[sangbeenmoon] WEEK 08 Solutions #2820
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,59 @@ | ||
|
|
||
| // Definition for a Node. | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
|
|
||
| /* | ||
| 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 { | ||
|
|
||
| // <원본, 복제본> | ||
| public Map<Node,Node> map = new HashMap<>(); | ||
|
|
||
| public Node cloneGraph(Node node) { | ||
|
|
||
| if (node == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return dfs(node); | ||
| } | ||
|
|
||
| public Node dfs(Node origin) { | ||
| if (map.containsKey(origin)) { | ||
| return map.get(origin); | ||
| } | ||
|
|
||
| if (origin == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Node copied = new Node(origin.val); | ||
| map.put(origin, copied); | ||
|
|
||
| for (Node n : origin.neighbors) { | ||
| Node neighbor = dfs(n); | ||
| copied.neighbors.add(neighbor); | ||
| } | ||
|
|
||
| return copied; | ||
| } | ||
| } |
|
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/sangbeenmoon.javaclass Solution {
public int longestCommonSubsequence(String text1, String text2) {
int dp[][] = new int[text1.length()][text2.length()];
for(int i = 0; i < text1.length(); i++){
if (text1.substring(0,i + 1).contains(String.valueOf(text2.charAt(0)))) {
dp[i][0] = 1;
}
}
for(int i = 0; i < text2.length(); i++){
if (text2.substring(0,i + 1).contains(String.valueOf(text1.charAt(0)))) {
dp[0][i] = 1;
}
}
for(int i = 1; i < text1.length(); i++){
for(int j = 1; j< text2.length(); j++){
if(text1.charAt(i) == text2.charAt(j)){
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[text1.length() - 1][text2.length() - 1];
}
}
📊 시간/공간 복잡도 분석
피드백: 2차원 DP 배열을 사용해 모든 부분문제 결과를 저장한다. 개선 제안: 경계 초기화와 인덱스 접근을 명확히 하고, 공간을 줄이려면 한 행만 사용하는 방법도 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Solution { | ||
| public int longestCommonSubsequence(String text1, String text2) { | ||
|
|
||
| int dp[][] = new int[text1.length()][text2.length()]; | ||
|
|
||
|
|
||
| for(int i = 0; i < text1.length(); i++){ | ||
| if (text1.substring(0,i + 1).contains(String.valueOf(text2.charAt(0)))) { | ||
| dp[i][0] = 1; | ||
| } | ||
| } | ||
|
|
||
| for(int i = 0; i < text2.length(); i++){ | ||
| if (text2.substring(0,i + 1).contains(String.valueOf(text1.charAt(0)))) { | ||
| dp[0][i] = 1; | ||
| } | ||
| } | ||
|
|
||
| for(int i = 1; i < text1.length(); i++){ | ||
| for(int j = 1; j< text2.length(); j++){ | ||
| if(text1.charAt(i) == text2.charAt(j)){ | ||
| 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[text1.length() - 1][text2.length() - 1]; | ||
|
|
||
|
|
||
| } | ||
| } |
|
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/sangbeenmoon.javaimport java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Solution {
Map<Character, Integer> counterMap = new HashMap<>();
public int characterReplacement(String s, int k) {
int left = 0;
int answer = 0;
for (int right = 0; right < s.length(); right++){
if(counterMap.containsKey(s.charAt(right))){
int cnt = counterMap.get(s.charAt(right));
counterMap.put(s.charAt(right), cnt + 1);
}
else {
counterMap.put(s.charAt(right), 1);
}
while (!isCounterOK(k)) {
char l = s.charAt(left);
int cnt = counterMap.get(l);
if (cnt == 1) counterMap.remove(l);
else counterMap.put(l, cnt - 1);
left++;
}
answer = Math.max(answer, right - left + 1);
}
return answer;
}
public boolean isCounterOK(int k) {
int maxCount = 0;
int totalCount = 0;
for (Entry<Character, Integer> entry : counterMap.entrySet()) {
maxCount = Math.max(maxCount, entry.getValue());
totalCount = totalCount + entry.getValue();
}
return totalCount - maxCount <= k;
}
}
📊 시간/공간 복잡도 분석
피드백: 윈도우의 크기를 유지하며 최대 빈도 문자를 활용한다. 개선 제안: Counter 갱신 로직을 간결화하고, maxCount를 window 밖에서도 관리하는 방법을 고려해볼 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
|
|
||
| class Solution { | ||
|
|
||
| Map<Character, Integer> counterMap = new HashMap<>(); | ||
|
|
||
| public int characterReplacement(String s, int k) { | ||
|
|
||
| int left = 0; | ||
| int answer = 0; | ||
|
|
||
| for (int right = 0; right < s.length(); right++){ | ||
| if(counterMap.containsKey(s.charAt(right))){ | ||
|
|
||
| int cnt = counterMap.get(s.charAt(right)); | ||
| counterMap.put(s.charAt(right), cnt + 1); | ||
| } | ||
| else { | ||
| counterMap.put(s.charAt(right), 1); | ||
| } | ||
|
|
||
| while (!isCounterOK(k)) { | ||
| char l = s.charAt(left); | ||
| int cnt = counterMap.get(l); | ||
| if (cnt == 1) counterMap.remove(l); | ||
| else counterMap.put(l, cnt - 1); | ||
| left++; | ||
| } | ||
| answer = Math.max(answer, right - left + 1); | ||
|
Comment on lines
+15
to
+31
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. 돌려보니 성능이 좋은 편은 아니라서 조금 더 최적화 시도해보셔도 좋을 거 같습니다. |
||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| public boolean isCounterOK(int k) { | ||
| int maxCount = 0; | ||
| int totalCount = 0; | ||
| for (Entry<Character, Integer> entry : counterMap.entrySet()) { | ||
| maxCount = Math.max(maxCount, entry.getValue()); | ||
| totalCount = totalCount + entry.getValue(); | ||
| } | ||
| return totalCount - maxCount <= k; | ||
| } | ||
|
Comment on lines
+37
to
+45
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
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. 🏷️ 알고리즘 패턴 분석palindromic-substrings/sangbeenmoon.pyclass Solution:
def countSubstrings(self, s: str) -> int:
dp = [[0] * (len(s) + 1) for _ in range(len(s) + 1)]
def isPalindrome(start, end) -> bool:
if end - start == 0:
return True
if end - start == 1 :
return s[start] == s[end]
if dp[start][end] == 1:
return True
if dp[start][end] == -1:
return False
if s[start] == s[end]:
return isPalindrome(start+1, end-1)
return False
answer = 0
for i in range(len(s) - 1, -1, -1):
for j in range(i,len(s)):
if isPalindrome(i,j):
dp[i][j] = 1
answer += 1
else:
dp[i][j] = -1
return answer
📊 시간/공간 복잡도 분석
피드백: isPalindrome 재귀와 DP 표를 섞어 부분 문자열의 회문 여부를 저장한다. 개선 제안: 현재 구현은 Python 구문과 DP 표 초기화가 다소 비효율적일 수 있어, 확정된 확장 방법으로 단순화하는 것을 고려해볼 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class Solution: | ||
| def countSubstrings(self, s: str) -> int: | ||
| dp = [[0] * (len(s) + 1) for _ in range(len(s) + 1)] | ||
|
|
||
| def isPalindrome(start, end) -> bool: | ||
|
|
||
| if end - start == 0: | ||
| return True | ||
|
|
||
| if end - start == 1 : | ||
| return s[start] == s[end] | ||
|
|
||
| if dp[start][end] == 1: | ||
| return True | ||
|
|
||
| if dp[start][end] == -1: | ||
| return False | ||
|
|
||
| if s[start] == s[end]: | ||
| return isPalindrome(start+1, end-1) | ||
|
|
||
| return False | ||
|
|
||
| answer = 0 | ||
|
|
||
| for i in range(len(s) - 1, -1, -1): | ||
| for j in range(i,len(s)): | ||
| if isPalindrome(i,j): | ||
| dp[i][j] = 1 | ||
| answer += 1 | ||
| else: | ||
| dp[i][j] = -1 | ||
|
|
||
| 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/sangbeenmoon.java
📊 시간/공간 복잡도 분석
피드백: 맵으로 원본 노드와 복제본 노드를 매핑해 사이클이 있는 그래프에서도 중복 복제를 방지한다.
개선 제안: 현재 구현이 적절해 보입니다.