diff --git a/container-with-most-water/TonyKim9401.java b/container-with-most-water/TonyKim9401.java new file mode 100644 index 00000000..e3a24492 --- /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; + } +} 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 00000000..ed3f0986 --- /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]); + } + } +} diff --git a/longest-increasing-subsequence/TonyKim9401.java b/longest-increasing-subsequence/TonyKim9401.java new file mode 100644 index 00000000..e26083a9 --- /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(); + } +} diff --git a/spiral-matrix/TonyKim9401.java b/spiral-matrix/TonyKim9401.java new file mode 100644 index 00000000..9f36f50a --- /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; + } +} diff --git a/valid-parentheses/TonyKim9401.java b/valid-parentheses/TonyKim9401.java new file mode 100644 index 00000000..c3c88b54 --- /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(); + } +}