Skip to content

[sora0319] Week 08 solutions #1514

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 5 commits into from
May 25, 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
41 changes: 41 additions & 0 deletions clone-graph/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
// Definition for a Node.
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 {
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 clone = new Node(node.val);
visited.put(node, clone);

for (Node neighbor : node.neighbors) {
clone.neighbors.add(cloneGraph(neighbor));
}

return clone;
}
}

24 changes: 24 additions & 0 deletions longest-common-subsequence/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int M = text1.length();
int 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)) {
// 문자가 일치하면 대각선 값 + 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];
}
}

29 changes: 29 additions & 0 deletions longest-repeating-character-replacement/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.HashMap;
import java.util.Map;

public class Solution {
public int characterReplacement(String s, int k) {
int maxLen = 0;
int maxCount = 0;
Map<Character, Integer> counter = new HashMap<>();
int start = 0;

for (int end = 0; end < s.length(); end++) {
char endChar = s.charAt(end);
counter.put(endChar, counter.getOrDefault(endChar, 0) + 1);
maxCount = Math.max(maxCount, counter.get(endChar));

while (end - start + 1 - maxCount > k) {
char startChar = s.charAt(start);
counter.put(startChar, counter.get(startChar) - 1);
start++;
}

maxLen = Math.max(maxLen, end - start + 1);
}

return maxLen;
}
}


19 changes: 19 additions & 0 deletions palindromic-substrings/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int countSubstrings(String s) {
int count = 0;

for (int i = 0; i < 2 * s.length() - 1; i++) {
int left = i / 2;
int right = left + i % 2;

while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
count++;
left--;
right++;
}
}

return count;
}
}

14 changes: 14 additions & 0 deletions reverse-bits/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
String binary = String.format("%32s", Integer.toBinaryString(n)).replace(' ', '0');
System.out.println(binary);

StringBuilder sb = new StringBuilder(binary);
sb.reverse();


return Integer.parseUnsignedInt(sb.toString(),2);
}
}