From ac768787efba07e99c5ca43d26f1a9c527e2416b Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 16 Sep 2024 14:34:45 -0400 Subject: [PATCH 1/5] Valid Parentheses --- valid-parentheses/TonyKim9401.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 valid-parentheses/TonyKim9401.java diff --git a/valid-parentheses/TonyKim9401.java b/valid-parentheses/TonyKim9401.java new file mode 100644 index 000000000..c3c88b541 --- /dev/null +++ b/valid-parentheses/TonyKim9401.java @@ -0,0 +1,18 @@ +// TC: O(n) +// -> n = s.length +// SC: O(n) +// -> n = s.length / 2 +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + + for (char c : s.toCharArray()) { + if (c == '(') stack.add(')'); + else if (c == '{') stack.add('}'); + else if (c == '[') stack.add(']'); + else if (stack.isEmpty() || stack.pop() != c) return false; + } + + return stack.isEmpty(); + } +} From b6925737d0bd43a37e58cbc3c392413012519d5b Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 17 Sep 2024 14:26:51 -0400 Subject: [PATCH 2/5] Container With Most Water --- container-with-most-water/TonyKim9401.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 container-with-most-water/TonyKim9401.java diff --git a/container-with-most-water/TonyKim9401.java b/container-with-most-water/TonyKim9401.java new file mode 100644 index 000000000..e3a24492b --- /dev/null +++ b/container-with-most-water/TonyKim9401.java @@ -0,0 +1,24 @@ +// TC: +// SC: +class Solution { + public int maxArea(int[] height) { + int max = 0; + + int start = 0; + int end = height.length-1; + + while (start < end) { + int heightLeft = height[start]; + int heightRight = height[end]; + + int hei = Math.min(heightLeft, heightRight); + int wid = end - start; + + max = Math.max(max, hei*wid); + + if (heightRight > heightLeft) start += 1; + else end -= 1; + } + return max; + } +} From c19845fe825f78ec3e883ce7761e927290519a5c Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 18 Sep 2024 15:32:49 -0400 Subject: [PATCH 3/5] Design add and search words structure --- .../TonyKim9401.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 design-add-and-search-words-data-structure/TonyKim9401.java diff --git a/design-add-and-search-words-data-structure/TonyKim9401.java b/design-add-and-search-words-data-structure/TonyKim9401.java new file mode 100644 index 000000000..ed3f09866 --- /dev/null +++ b/design-add-and-search-words-data-structure/TonyKim9401.java @@ -0,0 +1,56 @@ +// SC: O(n) +// -> n is the length of the given String +// TC: O(n * 26) +// -> n is the length of the given String * the number of alphabets +class TrieNode { + TrieNode[] childNode; + boolean isEndOfWord; + + public TrieNode() { + childNode = new TrieNode[26]; + isEndOfWord = false; + } +} + +class WordDictionary { + + private TrieNode root; + + public WordDictionary() { + root = new TrieNode(); + } + + public void addWord(String word) { + TrieNode node = root; + + for (char c : word.toCharArray()) { + int idx = c - 'a'; + if (node.childNode[idx] == null) { + node.childNode[idx] = new TrieNode(); + } + node = node.childNode[idx]; + } + node.isEndOfWord = true; + } + + public boolean search(String word) { + return searchInNode(word.toCharArray(), 0, root); + } + + private boolean searchInNode(char[] word, int idx, TrieNode node) { + if (idx == word.length) return node.isEndOfWord; + + char c = word[idx]; + + if (c == '.') { + for (TrieNode child : node.childNode) { + if (child != null && searchInNode(word, idx+1, child)) return true; + } + return false; + } else { + int childIdx = c - 'a'; + if (node.childNode[childIdx] == null) return false; + return searchInNode(word, idx+1, node.childNode[childIdx]); + } + } +} From 5eeaa46cd9e0c8b13c99a2f9e2a70027c5950f02 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 20 Sep 2024 10:38:15 -0400 Subject: [PATCH 4/5] Longest Increasing Subsequence --- .../TonyKim9401.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-increasing-subsequence/TonyKim9401.java diff --git a/longest-increasing-subsequence/TonyKim9401.java b/longest-increasing-subsequence/TonyKim9401.java new file mode 100644 index 000000000..e26083a97 --- /dev/null +++ b/longest-increasing-subsequence/TonyKim9401.java @@ -0,0 +1,22 @@ +// TC: O(n log n) +// -> nums for loop O(n) + binarySearch O(log n) +// SC: O(n) +// -> ArrayList could have nums all elements +class Solution { + public int lengthOfLIS(int[] nums) { + List output = new ArrayList<>(); + + for (int num : nums) { + int start = 0; + int end = output.size(); + while (start < end) { + int mid = start + (end - start) / 2; + if (output.get(mid) < num) start = mid + 1; + else end = mid; + } + if (start == output.size()) output.add(num); + else output.set(start, num); + } + return output.size(); + } +} From d92c799fa150777549a9480167c957137a9844e5 Mon Sep 17 00:00:00 2001 From: ktony Date: Fri, 20 Sep 2024 10:38:25 -0400 Subject: [PATCH 5/5] Spiral Matrix --- spiral-matrix/TonyKim9401.java | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 spiral-matrix/TonyKim9401.java diff --git a/spiral-matrix/TonyKim9401.java b/spiral-matrix/TonyKim9401.java new file mode 100644 index 000000000..9f36f50ad --- /dev/null +++ b/spiral-matrix/TonyKim9401.java @@ -0,0 +1,44 @@ +class Solution { + public List spiralOrder(int[][] matrix) { + List output = new ArrayList<>(); + int north = 0; + int south = matrix.length - 1; + int east = matrix[0].length - 1; + int west = 0; + + while (north <= south && west <= east) { + int j = west; + while (j <= east) { + output.add(matrix[north][j]); + j += 1; + } + north += 1; + + int i = north; + while (i <= south) { + output.add(matrix[i][east]); + i += 1; + } + east -= 1; + + if (north <= south) { + j = east; + while (j >= west) { + output.add(matrix[south][j]); + j -= 1; + } + south -= 1; + } + + if (west <= east) { + i = south; + while (i >= north) { + output.add(matrix[i][west]); + i -= 1; + } + west += 1; + } + } + return output; + } +}