Skip to content

[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 2 commits into from
Feb 2, 2025
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
22 changes: 22 additions & 0 deletions clone-graph/imsosleepy.java
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;
}
}
20 changes: 20 additions & 0 deletions longest-common-subsequence/imsosleepy.java
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];
}
}
24 changes: 24 additions & 0 deletions longest-repeating-character-replacement/imsosleepy.java
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;
}
}
19 changes: 19 additions & 0 deletions number-of-1-bits/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 가장 먼저 떠오른 방법이지만 권장되지 않는 방법일 것 같음
// 변환된 이진 문자열을 치환하는 방식은 O(K + logN)이다.
class Solution {
public int hammingWeight(int n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런걸 hammingWeight라고 부르는 군요 ㅎㅎ 잘 배워갑니다

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;
}
}
18 changes: 18 additions & 0 deletions sum-of-two-integers/imsosleepy.java
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}