diff --git a/longest-consecutive-sequence/TonyKim9401.java b/longest-consecutive-sequence/TonyKim9401.java new file mode 100644 index 000000000..a52a70146 --- /dev/null +++ b/longest-consecutive-sequence/TonyKim9401.java @@ -0,0 +1,21 @@ +// TC: O(n) +// SC: O(n) +class Solution { + public int longestConsecutive(int[] nums) { + int output = 0; + Set<Integer> set = new HashSet<>(); + + for (int num : nums) set.add(num); + + for (int num : nums) { + int count = 1; + if (!set.contains(num - count)){ + while (set.contains(num + count)) { + count += 1; + } + } + output = Math.max(output, count); + } + return output; + } +} diff --git a/maximum-product-subarray/TonyKim9401.java b/maximum-product-subarray/TonyKim9401.java new file mode 100644 index 000000000..9fb5b407c --- /dev/null +++ b/maximum-product-subarray/TonyKim9401.java @@ -0,0 +1,24 @@ +// TC: O(n) +// SC: O(1) +class Solution { + public int maxProduct(int[] nums) { + int currentMax = nums[0]; + int currentMin = nums[0]; + int maxProduct = nums[0]; + + for (int i = 1; i < nums.length; i++) { + if (nums[i] < 0) { + int temp = currentMax; + currentMax = currentMin; + currentMin = temp; + } + + currentMax = Math.max(nums[i], currentMax * nums[i]); + currentMin = Math.min(nums[i], currentMin * nums[i]); + + maxProduct = Math.max(maxProduct, currentMax); + } + + return maxProduct; + } +} diff --git a/missing-number/TonyKim9401.java b/missing-number/TonyKim9401.java new file mode 100644 index 000000000..6419aa82c --- /dev/null +++ b/missing-number/TonyKim9401.java @@ -0,0 +1,15 @@ +// TC: O(n) +// -> add all nums into set +// SC: O(n) +// -> set contains all nums' elements +class Solution { + public int missingNumber(int[] nums) { + Set<Integer> set = new HashSet<>(); + for (int num : nums) set.add(num); + + int output = 0; + while (set.contains(output)) output += 1; + + return output; + } +} diff --git a/valid-palindrome/TonyKim9401.java b/valid-palindrome/TonyKim9401.java new file mode 100644 index 000000000..e1b13cd17 --- /dev/null +++ b/valid-palindrome/TonyKim9401.java @@ -0,0 +1,21 @@ +// TC: O(n) +// SC: O(1) +class Solution { + public boolean isPalindrome(String s) { + int start = 0; + int end = s.length() - 1; + + while (start < end) { + while (!Character.isLetterOrDigit(s.charAt(start)) && start < end) start += 1; + while (!Character.isLetterOrDigit(s.charAt(end)) && start < end) end -= 1; + + if (Character.toLowerCase(s.charAt(start)) + != Character.toLowerCase( s.charAt(end))) return false; + + start += 1; + end -= 1; + } + + return true; + } +} diff --git a/word-search/TonyKim9401.java b/word-search/TonyKim9401.java new file mode 100644 index 000000000..23c6d61be --- /dev/null +++ b/word-search/TonyKim9401.java @@ -0,0 +1,51 @@ +// TC: O(n * m * 4^k); +// -> The size of board: n * m +// -> Check 4 directions by the given word's length: 4^k +// SC: O(n * m + k) +// -> boolean 2D array: n * M +// -> recursive max k spaces +class Solution { + public boolean exist(char[][] board, String word) { + // Mark visited path to do not go back. + boolean[][] visit = new boolean[board.length][board[0].length]; + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (wordSearch(i, j, 0, word, board, visit)) return true; + } + } + return false; + } + + private boolean wordSearch(int i, int j, int idx, String word, char[][] board, boolean[][] visit) { + + // When idx checking reach to the end of the length of the word then, return true + if (idx == word.length()) return true; + + // Check if i and j are inside of the range + if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false; + + // Check if the coordinate equals to the charactor value + if (board[i][j] != word.charAt(idx)) return false; + if (visit[i][j]) return false; + + // Mark the coordinate as visited + visit[i][j] = true; + + // If visited, the target is gonna be the next charactor + idx += 1; + + // If any direction returns true then it is true + if ( + wordSearch(i+1, j, idx, word, board, visit) || + wordSearch(i-1, j, idx, word, board, visit) || + wordSearch(i, j+1, idx, word, board, visit) || + wordSearch(i, j-1, idx, word, board, visit) + ) return true; + + // If visited wrong direction, turns it as false + visit[i][j] = false; + + return false; + } +}