diff --git a/clone-graph/TonyKim9401.java b/clone-graph/TonyKim9401.java new file mode 100644 index 000000000..fc691aa38 --- /dev/null +++ b/clone-graph/TonyKim9401.java @@ -0,0 +1,19 @@ +// TC: O(n) +// -> visit all elements once for each to clone them +// SC: O(n) +// -> all elements are stored in HashMap and values are limited maximum 2 +class Solution { + private Map map = new HashMap<>(); + public Node cloneGraph(Node node) { + if (node == null) return null; + if (map.containsKey(node.val)) return map.get(node.val); + + Node clone = new Node(node.val); + map.put(clone.val, clone); + + for (Node neighbor : node.neighbors) { + clone.neighbors.add(cloneGraph(neighbor)); + } + return clone; + } +} diff --git a/longest-common-subsequence/TonyKim9401.java b/longest-common-subsequence/TonyKim9401.java new file mode 100644 index 000000000..f6ee1154b --- /dev/null +++ b/longest-common-subsequence/TonyKim9401.java @@ -0,0 +1,23 @@ +// TC: O(n * m) +// the length of text1 by the length of text2 +// SC: O(n) +// both size of text1 and text2 can be the size of dp +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int[] dp = new int[text2.length()]; + int output = 0; + + for (char c : text1.toCharArray()) { + int curLength = 0; + + for (int i = 0; i < dp.length; i++) { + if (curLength < dp[i]) curLength = dp[i]; + else if (c == text2.charAt(i)) { + dp[i] = curLength + 1; + output = Math.max(output, dp[i]); + } + } + } + return output; + } +} diff --git a/longest-repeating-character-replacement/TonyKim9401.java b/longest-repeating-character-replacement/TonyKim9401.java new file mode 100644 index 000000000..3f0c71323 --- /dev/null +++ b/longest-repeating-character-replacement/TonyKim9401.java @@ -0,0 +1,27 @@ +// TC: O(26 * n) => O(n) +// iterates 26 times at the first for-loop, while loop O(n) +// SC: O(1) +class Solution { + public int characterReplacement(String s, int k) { + int ans = 0; + int n = s.length(); + for (char c = 'A'; c <= 'Z'; c++) { + int i = 0, j = 0, replaced = 0; + while (j < n) { + if (s.charAt(j) == c) { + j += 1; + } else if (replaced < k) { + j += 1; + replaced++; + } else if (s.charAt(i) == c) { + i += 1; + } else { + i += 1; + replaced -= 1; + } + ans = Math.max(ans, j - i); + } + } + return ans; + } +} diff --git a/merge-two-sorted-lists/TonyKim9401.java b/merge-two-sorted-lists/TonyKim9401.java new file mode 100644 index 000000000..1f0b176fd --- /dev/null +++ b/merge-two-sorted-lists/TonyKim9401.java @@ -0,0 +1,26 @@ +// TC: O(n) +// n = length sum of list1 and list2 +// SC: O(n) +// n = node 0 ~ length sum of list1 and list2 +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode node = new ListNode(0); + ListNode output = node; + + while (list1 != null && list2 != null) { + if (list1.val > list2.val) { + node.next = list2; + list2 = list2.next; + } else { + node.next = list1; + list1 = list1.next; + } + node = node.next; + } + + if (list1 == null) node.next = list2; + if (list2 == null) node.next = list1; + + return output.next; + } +} diff --git a/sum-of-two-integers/TonyKim9401.java b/sum-of-two-integers/TonyKim9401.java new file mode 100644 index 000000000..5463bf7e9 --- /dev/null +++ b/sum-of-two-integers/TonyKim9401.java @@ -0,0 +1,12 @@ +// TC: O(1) +// SC: O(1) +class Solution { + public int getSum(int a, int b) { + while (b != 0) { + int temp = (a & b) << 1; + a = a^b; + b = temp; + } + return a; + } +}