diff --git a/clone-graph/Tessa1217.java b/clone-graph/Tessa1217.java new file mode 100644 index 000000000..ad9fcf607 --- /dev/null +++ b/clone-graph/Tessa1217.java @@ -0,0 +1,59 @@ +/* +// Definition for a Node. +class Node { + public int val; + public List neighbors; + public Node() { + val = 0; + neighbors = new ArrayList(); + } + public Node(int _val) { + val = _val; + neighbors = new ArrayList(); + } + public Node(int _val, ArrayList _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ + +import java.util.HashMap; +import java.util.Map; + +/** + * 참조 노드는 무방향 그래프에 연결되어있다. 그래프의 deep copy(clone)을 반환하세요. + */ +class Solution { + + // 방문한 노드를 기억할 Map 선언 + Map visited = new HashMap<>(); + + public Node cloneGraph(Node node) { + return clone(node); + } + + public Node clone(Node node) { + if (node == null) { + return null; + } + + // 이미 방문했으면 Map에서 꺼내서 반환 + if (visited.containsKey(node)) { + return visited.get(node); + } + + // 신규 Node 생성 + Node newNode = new Node(node.val); + visited.put(node, newNode); + + // 인접 노드 Clone + if (node.neighbors != null && !node.neighbors.isEmpty()) { + for (Node neighbor : node.neighbors) { + newNode.neighbors.add(clone(neighbor)); + } + } + return newNode; + } +} + diff --git a/longest-common-subsequence/Tessa1217.java b/longest-common-subsequence/Tessa1217.java new file mode 100644 index 000000000..fc361ded2 --- /dev/null +++ b/longest-common-subsequence/Tessa1217.java @@ -0,0 +1,27 @@ +/** + * 두 문자열 text1과 text2가 주어질 때 가장 긴 공통 부분 수열의 길이를 리턴하고 없으면 0을 리턴하세요. + */ +class Solution { + + // 시간복잡도: O(t1Length * t2Length) + public int longestCommonSubsequence(String text1, String text2) { + + int t1Length = text1.length(); + int t2Length = text2.length(); + + int[][]dp = new int[t1Length + 1][t2Length + 1]; + + for (int i = 1; i <= t1Length; i++) { + for (int j = 1; j <= t2Length; 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[t1Length][t2Length]; + + } +} + diff --git a/longest-repeating-character-replacement/Tessa1217.java b/longest-repeating-character-replacement/Tessa1217.java new file mode 100644 index 000000000..f3a5b64ad --- /dev/null +++ b/longest-repeating-character-replacement/Tessa1217.java @@ -0,0 +1,35 @@ +/** + * 문자열 s와 정수 k가 주어진다. + * 문자열 내 아무 문자나 대문자 영문자로 변경할 수 있으며 해당 연산을 k번까지 할 수 있을 때, + * 같은 문자를 포함하는 가장 긴 부분 수열을 반환하세요. + */ +class Solution { + + // 시간복잡도: O(n) + public int characterReplacement(String s, int k) { + + int[] chars = new int[26]; + int left = 0; + int maxLength = 0; + int maxCnt = 0; + + for (int right = 0; right < s.length(); right++) { + int charIdx = s.charAt(right) - 'A'; + chars[charIdx]++; + // 현재 윈도우 내에서 가장 많이 등장하는 문자의 개수 + maxCnt = Math.max(chars[charIdx], maxCnt); + + // window size - max cnt > k (window 사이즈 조정) + while (right - left + 1 - maxCnt > k) { + chars[s.charAt(left) - 'A']--; + left++; + } + + maxLength = Math.max(maxLength, right - left + 1); + } + + return maxLength; + + } +} + diff --git a/palindromic-substrings/Tessa1217.java b/palindromic-substrings/Tessa1217.java new file mode 100644 index 000000000..31170a4f2 --- /dev/null +++ b/palindromic-substrings/Tessa1217.java @@ -0,0 +1,29 @@ +/** + * palindrome 부분 수열의 개수를 찾으세요. + */ +class Solution { + + /** 시간복잡도: O(n^2), O(1) */ + public int countSubstrings(String s) { + + int count = 0; + + for (int i = 0; i < s.length(); i++) { + count += checkPalindrome(s, i, i); // 홀수 + count += checkPalindrome(s, i, i + 1); // 짝수 + } + + return count; + } + + private int checkPalindrome(String s, int left, int right) { + int count = 0; + while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { + count++; + left--; + right++; + } + return count; + } +} + diff --git a/reverse-bits/Tessa1217.java b/reverse-bits/Tessa1217.java new file mode 100644 index 000000000..f266a7dd8 --- /dev/null +++ b/reverse-bits/Tessa1217.java @@ -0,0 +1,33 @@ +public class Solution { + + // 비트 연산자 사용 + public int reverseBits(int n) { + int answer = 0; + for (int i = 0; i < 32; i++) { + answer <<= 1; + answer |= (n & 1); + n >>>= 1; + } + + return answer; + } + + // you need treat n as an unsigned value + // O(1) + // public int reverseBits(int n) { + + // int answer = 0; + + // String binary = Integer.toBinaryString(n); + + // StringBuilder padded = new StringBuilder(); + // for (int i = 0; i < 32 - binary.length(); i++) { + // padded.append('0'); + // } + // padded.append(binary); + + // return (int) Long.parseLong(padded.reverse().toString(), 2); + + // } +} +