Skip to content

Commit fd46505

Browse files
authored
Merge pull request #972 from imsosleepy/main
[Ackku] week 8
2 parents a92b14b + 260d1cd commit fd46505

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

โ€Žclone-graph/imsosleepy.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ๋ฌธ์ œ ์˜๋„๋ฅผ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Œ. ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ๋œ Node๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.
2+
// DFS๋กœ ํ’€๋ฉด ๋˜๊ณ , BFS๋„ ๊ฐ€๋Šฅํ•˜๊ฒ ์ง€๋งŒ BFS๋Š” ๊นŒ๋”ฑ ์ž˜๋ชปํ•˜๋ฉด ํƒ€์ž„์•„์›ƒ์ด ๋‚˜์„œ ๊ทธ๋ƒฅ DFS๋กœ ํ•ด๊ฒฐ
3+
class Solution {
4+
private Map<Node, Node> visited = new HashMap<>();
5+
6+
public Node cloneGraph(Node node) {
7+
if (node == null) return null;
8+
9+
if (visited.containsKey(node)) {
10+
return visited.get(node);
11+
}
12+
13+
Node cloneNode = new Node(node.val);
14+
visited.put(node, cloneNode);
15+
16+
for (Node neighbor : node.neighbors) {
17+
cloneNode.neighbors.add(cloneGraph(neighbor));
18+
}
19+
20+
return cloneNode;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ๊ณตํ†ต ๋ถ€๋ถ„ ์ˆ˜์—ด ๋ฌธ์ œ๋Š” ๋˜๊ฒŒ ์œ ๋ช…ํ•œ ๋ฌธ์ œ์ž„
2+
// ์ผ๋ฐ˜์ ์ธ DP๋Š” O(N)์ธ๋ฐ, ์ด๊ฑด O(N^2)์ด๋ผ ์–ด๋ ค์›€
3+
// GPT์˜ ๋„์›€์„ ๋ฐ›์•˜์Œ. ๋”ฐ๋กœ ์ •๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ๋“ฏ
4+
class Solution {
5+
public int longestCommonSubsequence(String text1, String text2) {
6+
int m = text1.length(), n = text2.length();
7+
int[][] dp = new int[m + 1][n + 1];
8+
9+
for (int i = 1; i <= m; i++) {
10+
for (int j = 1; j <= n; j++) {
11+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
12+
dp[i][j] = dp[i - 1][j - 1] + 1;
13+
} else {
14+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
15+
}
16+
}
17+
}
18+
return dp[m][n];
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ์ตœ๊ทผ ์ž์ฃผ ๋ณด์˜€๋˜ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•
2+
// ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N)
3+
class Solution {
4+
public int characterReplacement(String s, int k) {
5+
int left = 0, maxLen = 0;
6+
int[] count = new int[26];
7+
int maxCount = 0;
8+
9+
for (int right = 0; right < s.length(); right++) {
10+
count[s.charAt(right) - 'A']++; // ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž ์ถ”๊ฐ€
11+
maxCount = Math.max(maxCount, count[s.charAt(right) - 'A']); // ์ตœ๋นˆ ๋ฌธ์ž ์—…๋ฐ์ดํŠธ
12+
13+
// ํ˜„์žฌ ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ - ์ตœ๋นˆ ๋ฌธ์ž ๊ฐœ์ˆ˜ > k๋ผ๋ฉด ์œˆ๋„์šฐ๋ฅผ ์ถ•์†Œ
14+
while ((right - left + 1) - maxCount > k) {
15+
count[s.charAt(left) - 'A']--; // ์™ผ์ชฝ ๋ฌธ์ž ์ œ๊ฑฐ
16+
left++;
17+
}
18+
19+
maxLen = Math.max(maxLen, right - left + 1); // ์ตœ๋Œ€ ๊ธธ์ด ์—…๋ฐ์ดํŠธ
20+
}
21+
22+
return maxLen;
23+
}
24+
}

โ€Žnumber-of-1-bits/imsosleepy.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ค๋ฅธ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ผ ๊ฒƒ ๊ฐ™์Œ
2+
// ๋ณ€ํ™˜๋œ ์ด์ง„ ๋ฌธ์ž์—ด์„ ์น˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์€ O(K + logN)์ด๋‹ค.
3+
class Solution {
4+
public int hammingWeight(int n) {
5+
return Integer.toBinaryString(n).replace("0", "").length();
6+
}
7+
}
8+
9+
// ๊ทธ๋ƒฅ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋กœ ํ‘ธ๋Š”๊ฒŒ ์ตœ๊ณ ์ธ๊ฒƒ ๊ฐ™๋‹ค.
10+
class Solution {
11+
public int hammingWeight(int n) {
12+
int count = 0;
13+
while (n != 0) {
14+
count += (n & 1); // ๋งˆ์ง€๋ง‰ ์ˆซ์ž๊ฐ€ 1์ธ์ง€ ํ™•์ธ
15+
n >>>= 1; // n์„ 1์นธ ์˜ค๋ฅธ์ชฝ ์ด๋™
16+
}
17+
return count;
18+
}
19+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ œ๊ฐ€ ์ ‘ํ•˜๊ธฐ ์–ด๋ ต๊ธดํ•œ๋ฐ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค
2+
class Solution {
3+
public int getSum(int a, int b) {
4+
while (b != 0) {
5+
int carry = (a & b) << 1; // AND ์—ฐ์‚ฐ ํ›„ ์™ผ์ชฝ ์‰ฌํ”„ํŠธํ•˜์—ฌ ์ž๋ฆฌ ์˜ฌ๋ฆผ ๊ณ„์‚ฐ
6+
a = a ^ b; // XOR ์—ฐ์‚ฐ์œผ๋กœ ์ž๋ฆฌ ์˜ฌ๋ฆผ ์—†๋Š” ๋ง์…ˆ ์ˆ˜ํ–‰
7+
b = carry; // ์ž๋ฆฌ ์˜ฌ๋ฆผ์„ ๋‹ค์Œ ์—ฐ์‚ฐ์— ์‚ฌ์šฉ
8+
}
9+
return a;
10+
}
11+
}
12+
13+
// ์ด๋Ÿฐ์‹์œผ๋กœ๋„ ํ’€๋ฆฌ์ง€๋งŒ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋Š” ์›ํ•˜์ง€ ์•Š์„์ง€ ๋„..
14+
class Solution {
15+
public int getSum(int a, int b) {
16+
return Math.addExact(a, b);
17+
}
18+
}

0 commit comments

Comments
ย (0)