From dbbd78e9dc3eb3fd0d10a1c9588075d7f8a3aa0c Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 1 Feb 2025 20:29:05 +0900 Subject: [PATCH 1/5] feat : clone-graph --- clone-graph/ekgns33.java | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 clone-graph/ekgns33.java diff --git a/clone-graph/ekgns33.java b/clone-graph/ekgns33.java new file mode 100644 index 000000000..a14d18a84 --- /dev/null +++ b/clone-graph/ekgns33.java @@ -0,0 +1,42 @@ +/* +// 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; + } +} +*/ + +class Solution { + public Node cloneGraph(Node node) { + Map nodeMap = new HashMap<>(); + if(node == null) return null; + boolean[] visited = new boolean[101]; + dfsHelper(nodeMap, node, visited); + return nodeMap.get(1); + } + + private void dfsHelper(Map map, Node node, boolean[] visited) { + if(!map.containsKey(node.val)) { + map.put(node.val, new Node(node.val)); + } + visited[node.val] = true; + Node cur = map.get(node.val); + for(Node adjacent : node.neighbors) { + map.putIfAbsent(adjacent.val, new Node(adjacent.val)); + cur.neighbors.add(map.get(adjacent.val)); + if(!visited[adjacent.val]) dfsHelper(map, adjacent, visited); + } + } +} From ec955296b7c2f3a081078c9a4ba523514c95d6ee Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 1 Feb 2025 20:29:27 +0900 Subject: [PATCH 2/5] feat : longest-common-subsequence --- longest-common-subsequence/ekgns33.java | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 longest-common-subsequence/ekgns33.java diff --git a/longest-common-subsequence/ekgns33.java b/longest-common-subsequence/ekgns33.java new file mode 100644 index 000000000..5ce4fc151 --- /dev/null +++ b/longest-common-subsequence/ekgns33.java @@ -0,0 +1,76 @@ +/** + input : string text1, text2 + output : length of lcs else 0 + constraints: + 1) both input strings are not empty + 2) input strings are consist of lowercase eng. letters + + solution 1) brute force + + nested for loop + tc : O(n^2 * m) where n is min (len(text1), len(text2)), m is max + sc : O(1) + + solution 2) dp + + at each step we have 3 conditions + + 1. current characters are same. compare next characters + abcde. ith index + ^ + abc. jth index + ^ + then go to next and maximum length also increases + move (i + 1, j+1) + 2, 3. current characters are different. move i or j to compare + + abcde + ^ + abc + ^ + move (i +1, j) or (i, j+1) + + + let dp[i][j] max length of lcs + + a b c d e + a 1 1 1 1 1 + c 1 1 2 2 2 + e 1 1 2 2 3 + + a b d e f g h + a 1 1 1 1 1 1 1 + f 1 1 1 1 2 2 2 + h 1 1 1 1 2 2 3 + d 1 1 2 2 2 2 3 + + if equals + dp[i][j] = dp[i-1][j-1] + 1 + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + tc : O(mn) + sc : O(mn) > O(min(m, n)) if optimize space to 2-Row array + */ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int m = text1.length(), n = text2.length(); + if(m < n) { + return longestCommonSubsequence(text2, text1); + } + int[][] dp = new int[2][n+1]; + int prevRow = 0; + int curRow = 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[curRow][j] = dp[prevRow][j-1] + 1; + } else { + dp[curRow][j] = Math.max(dp[prevRow][j], dp[curRow][j-1]); + } + } + curRow = prevRow; + prevRow = (prevRow + 1) % 2; + } + return dp[prevRow][n]; + } +} From d38048264c3b4a1a71de8dafe7cd31ebe8683d4f Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 1 Feb 2025 20:29:42 +0900 Subject: [PATCH 3/5] feat : longest-repeating-character-replacement --- .../ekgns33.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 longest-repeating-character-replacement/ekgns33.java diff --git a/longest-repeating-character-replacement/ekgns33.java b/longest-repeating-character-replacement/ekgns33.java new file mode 100644 index 000000000..4e4ac7291 --- /dev/null +++ b/longest-repeating-character-replacement/ekgns33.java @@ -0,0 +1,119 @@ +/** + + input : String s + output : after k times operation, return length of longest substring + that consist of same letters + + example + AAAA << 4 + AAAB << AAA << 3 + + constraints: + 1) input string can be empty? + nope. at least one letter + 2) string s contains whitespace? non-alphabetical chars? + nope. only uppercase English letters + 3) range of k? + [0, n], when n is the length of string s + + ABAB + AAAB + AAAA + + AABABBA + AAAABBA + AABBBBA + + AABABBA + l + r + count : 1 + 4 + + solution 1: brute force + ds : array + algo : for loop + + nested for loop + iterate through the string s from index i = 0 to n + set current character as 'target' + set count = 0 + iterate through the string s from index j = i + 1 to n + if next character is identical to 'target' + continue; + else + increase count; + + if count > k + break and save length + + if count <=k compare length with max + ABAB + i + j + target = A + count = 1 + ABAB << count = 2, length = 4 + tc : O(n^2) + sc : O(1) + + solution 2: two pointer + + AABCABBA + l + r + A : 1 + B : 1 + C : 1 + count = 1 + res = 4 + set two pointer l and r + set count to 0; + set maxFrequency character s[0]; + + move r pointer to right + if current char is not maxFreqChar + count + 1; + else + continue; + + if count > k + while count <= k + move leftpointer right; + update frequency + set max Frequency character at each iteration O(26) + update count + + tc: O(n) + sc : O(26) ~= O(1) + */ + +class Solution { + public int characterReplacement(String s, int k) { + int l = 0, r = 1, maxFreq = 1, res = 1; + int n = s.length(); + int[] freq = new int[26]; + // base case; + char maxFreqChar = s.charAt(0); + freq[maxFreqChar - 'A']++; + // loop + while (r < n) { + char next = s.charAt(r); + maxFreq = Math.max(maxFreq, ++freq[next - 'A']); + + while(r - l + 1 - maxFreq > k) { + freq[s.charAt(l) - 'A']--; + l++; + for(int i = 0; i <26; i++) { + if(freq[i] > maxFreq) { + maxFreq = freq[i]; + } + } + } + res = Math.max(res, r - l+1); + r++; + } + + return res; + } +} From a86efc56cc3c9947a1f2a904c1a18c643dfb31fe Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 1 Feb 2025 20:29:56 +0900 Subject: [PATCH 4/5] feat : number-of-1-bits --- number-of-1-bits/ekgns33.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 number-of-1-bits/ekgns33.java diff --git a/number-of-1-bits/ekgns33.java b/number-of-1-bits/ekgns33.java new file mode 100644 index 000000000..a9cda052a --- /dev/null +++ b/number-of-1-bits/ekgns33.java @@ -0,0 +1,19 @@ +class Solution { + public int hammingWeight(int n) { + int curN = n; + int cnt = 0; + while(curN > 0) { + if(curN %2 == 1) { + cnt++; + } + curN /= 2; + } + return cnt; + } +} +/** + + 1 2 3 4 5 6 7 8 9 10 11 + 0 1 2 1 2 2 3 1 2 2 3 + + */ From cf1c9980eddc8987d56d83a72d6a38b3d02ebc11 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 1 Feb 2025 20:30:07 +0900 Subject: [PATCH 5/5] feat : sum-of-two-integers --- sum-of-two-integers/ekgns33.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sum-of-two-integers/ekgns33.java diff --git a/sum-of-two-integers/ekgns33.java b/sum-of-two-integers/ekgns33.java new file mode 100644 index 000000000..620e307f6 --- /dev/null +++ b/sum-of-two-integers/ekgns33.java @@ -0,0 +1,28 @@ +/* + +bit manipulation + +find carry of each binary digits sum +example + + 1 0 1 1 + +0 0 1 0 +----------- + 0 1 + ^carry 1 (1<<2) + 1 + 1 + + + */ +class Solution { + public int getSum(int a, int b) { + int carryBitSet; + while(b != 0) { + carryBitSet = a & b; + a = a ^ b; + b = carryBitSet << 1; + } + return a; + } +}