-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Ackku] week 8 #972
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
Merged
Merged
[Ackku] week 8 #972
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 문제 의도를 잘 모르겠음. 깊은 복사가 된 Node를 만들면 된다. | ||
// DFS로 풀면 되고, BFS도 가능하겠지만 BFS는 까딱 잘못하면 타임아웃이 나서 그냥 DFS로 해결 | ||
class Solution { | ||
private Map<Node, Node> visited = new HashMap<>(); | ||
|
||
public Node cloneGraph(Node node) { | ||
if (node == null) return null; | ||
|
||
if (visited.containsKey(node)) { | ||
return visited.get(node); | ||
} | ||
|
||
Node cloneNode = new Node(node.val); | ||
visited.put(node, cloneNode); | ||
|
||
for (Node neighbor : node.neighbors) { | ||
cloneNode.neighbors.add(cloneGraph(neighbor)); | ||
} | ||
|
||
return cloneNode; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 공통 부분 수열 문제는 되게 유명한 문제임 | ||
// 일반적인 DP는 O(N)인데, 이건 O(N^2)이라 어려움 | ||
// GPT의 도움을 받았음. 따로 정리가 필요할 듯 | ||
class Solution { | ||
public int longestCommonSubsequence(String text1, String text2) { | ||
int m = text1.length(), n = text2.length(); | ||
int[][] dp = new int[m + 1][n + 1]; | ||
|
||
for (int i = 1; i <= m; i++) { | ||
for (int j = 1; j <= n; 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[m][n]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 최근 자주 보였던 슬라이딩 윈도우를 이용한 방법 | ||
// 시간 복잡도 : O(N) | ||
class Solution { | ||
public int characterReplacement(String s, int k) { | ||
int left = 0, maxLen = 0; | ||
int[] count = new int[26]; | ||
int maxCount = 0; | ||
|
||
for (int right = 0; right < s.length(); right++) { | ||
count[s.charAt(right) - 'A']++; // 오른쪽 문자 추가 | ||
maxCount = Math.max(maxCount, count[s.charAt(right) - 'A']); // 최빈 문자 업데이트 | ||
|
||
// 현재 윈도우의 크기 - 최빈 문자 개수 > k라면 윈도우를 축소 | ||
while ((right - left + 1) - maxCount > k) { | ||
count[s.charAt(left) - 'A']--; // 왼쪽 문자 제거 | ||
left++; | ||
} | ||
|
||
maxLen = Math.max(maxLen, right - left + 1); // 최대 길이 업데이트 | ||
} | ||
|
||
return maxLen; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 가장 먼저 떠오른 방법이지만 권장되지 않는 방법일 것 같음 | ||
// 변환된 이진 문자열을 치환하는 방식은 O(K + logN)이다. | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
return Integer.toBinaryString(n).replace("0", "").length(); | ||
} | ||
} | ||
|
||
// 그냥 비트 연산자로 푸는게 최고인것 같다. | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n != 0) { | ||
count += (n & 1); // 마지막 숫자가 1인지 확인 | ||
n >>>= 1; // n을 1칸 오른쪽 이동 | ||
} | ||
return count; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 비트 연산자를 이용한 문제가 접하기 어렵긴한데 자주 나오는 것 같다 | ||
class Solution { | ||
public int getSum(int a, int b) { | ||
while (b != 0) { | ||
int carry = (a & b) << 1; // AND 연산 후 왼쪽 쉬프트하여 자리 올림 계산 | ||
a = a ^ b; // XOR 연산으로 자리 올림 없는 덧셈 수행 | ||
b = carry; // 자리 올림을 다음 연산에 사용 | ||
} | ||
return a; | ||
} | ||
} | ||
|
||
// 이런식으로도 풀리지만 이런 결과는 원하지 않을지 도.. | ||
class Solution { | ||
public int getSum(int a, int b) { | ||
return Math.addExact(a, b); | ||
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. |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이런걸 hammingWeight라고 부르는 군요 ㅎㅎ 잘 배워갑니다