From 51e59ce4477fce9cf09f03322118719583fa3dd4 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 00:45:24 -0400 Subject: [PATCH 1/9] Valid Palindrome --- valid-palindrome/TonyKim9401.java | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 valid-palindrome/TonyKim9401.java diff --git a/valid-palindrome/TonyKim9401.java b/valid-palindrome/TonyKim9401.java new file mode 100644 index 000000000..dee54c351 --- /dev/null +++ b/valid-palindrome/TonyKim9401.java @@ -0,0 +1,37 @@ +// TC: O(n) +// SC: O(n) +class Solution { + public boolean isPalindrome(String s) { + String target = checkString(s); + return checkPalindrome(target); + } + + private String checkString(String s) { + StringBuilder sb = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c >= 'a' && c <= 'z') { + sb.append(c); + } + if (c >= 'A' && c <= 'Z') { + c = (char)(c - 'A' + 'a'); + sb.append(c); + } + if (c >= '0' && c <= '9') { + sb.append(c); + } + } + return sb.toString(); + } + + private Boolean checkPalindrome(String target) { + int start = 0; + int end = target.length() - 1; + + while (start < end) { + if (target.charAt(start) != target.charAt(end)) return false; + start += 1; + end -= 1; + } + return true; + } +} From cc47d80e1ab4f4d8d44227c7ffb412bfe4c3cdc3 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 00:52:31 -0400 Subject: [PATCH 2/9] Missing Number --- missing-number/TonyKim9401.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 missing-number/TonyKim9401.java diff --git a/missing-number/TonyKim9401.java b/missing-number/TonyKim9401.java new file mode 100644 index 000000000..945c4ce19 --- /dev/null +++ b/missing-number/TonyKim9401.java @@ -0,0 +1,13 @@ +// TC: O(n log n) +// SC: O(n) +class Solution { + public int missingNumber(int[] nums) { + Arrays.sort(nums); + int idx = 1; + int n = nums.length; + for (; idx < n; idx++) { + if (nums[idx] - 1 != nums[idx-1]) return nums[idx] - 1; + } + return nums[idx-1] == n ? 0 : n; + } +} From ddc9916ff86de454d4fa478a63627e354d8b2c7c Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 00:53:06 -0400 Subject: [PATCH 3/9] Longest Consecutive Sequence --- longest-consecutive-sequence/TonyKim9401.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-consecutive-sequence/TonyKim9401.java 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 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; + } +} From 790350bb46e211165b8e516fd7c51cf8893f68ae Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 00:53:19 -0400 Subject: [PATCH 4/9] Word Search --- word-search/TonyKim9401.java | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 word-search/TonyKim9401.java diff --git a/word-search/TonyKim9401.java b/word-search/TonyKim9401.java new file mode 100644 index 000000000..ed9e714b2 --- /dev/null +++ b/word-search/TonyKim9401.java @@ -0,0 +1,52 @@ +// 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; + + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + visit = new boolean[board.length][board[0].length]; + 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; + } +} From ce5152f65071adcc4004b41cbee47872ee9236f7 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 00:53:40 -0400 Subject: [PATCH 5/9] Maximum Product Subarray --- maximum-product-subarray/TonyKim9401.java | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 maximum-product-subarray/TonyKim9401.java 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; + } +} From 03bcdebd328ef02c1af009c09bc791f497c79450 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 1 Sep 2024 18:36:05 -0400 Subject: [PATCH 6/9] Word Search visit object create only once --- word-search/TonyKim9401.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/word-search/TonyKim9401.java b/word-search/TonyKim9401.java index ed9e714b2..23c6d61be 100644 --- a/word-search/TonyKim9401.java +++ b/word-search/TonyKim9401.java @@ -7,11 +7,10 @@ class Solution { public boolean exist(char[][] board, String word) { // Mark visited path to do not go back. - boolean[][] visit; + 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++) { - visit = new boolean[board.length][board[0].length]; if (wordSearch(i, j, 0, word, board, visit)) return true; } } From a31c5f6559c43b9743d3de656bfa1f0994313795 Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 3 Sep 2024 01:56:37 -0400 Subject: [PATCH 7/9] Missing Number Reduced time complexity reflecting code review from Dale --- missing-number/TonyKim9401.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/missing-number/TonyKim9401.java b/missing-number/TonyKim9401.java index 945c4ce19..6419aa82c 100644 --- a/missing-number/TonyKim9401.java +++ b/missing-number/TonyKim9401.java @@ -1,13 +1,15 @@ -// TC: O(n log n) +// TC: O(n) +// -> add all nums into set // SC: O(n) +// -> set contains all nums' elements class Solution { public int missingNumber(int[] nums) { - Arrays.sort(nums); - int idx = 1; - int n = nums.length; - for (; idx < n; idx++) { - if (nums[idx] - 1 != nums[idx-1]) return nums[idx] - 1; - } - return nums[idx-1] == n ? 0 : n; + Set set = new HashSet<>(); + for (int num : nums) set.add(num); + + int output = 0; + while (set.contains(output)) output += 1; + + return output; } } From aaf34871480657907815ba7b41e319271e51fdb0 Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 3 Sep 2024 02:20:43 -0400 Subject: [PATCH 8/9] Valid Palindrome Code refactoring --- valid-palindrome/TonyKim9401.java | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/valid-palindrome/TonyKim9401.java b/valid-palindrome/TonyKim9401.java index dee54c351..ca747ebb6 100644 --- a/valid-palindrome/TonyKim9401.java +++ b/valid-palindrome/TonyKim9401.java @@ -2,35 +2,12 @@ // SC: O(n) class Solution { public boolean isPalindrome(String s) { - String target = checkString(s); - return checkPalindrome(target); - } - - private String checkString(String s) { - StringBuilder sb = new StringBuilder(); - for (char c : s.toCharArray()) { - if (c >= 'a' && c <= 'z') { - sb.append(c); - } - if (c >= 'A' && c <= 'Z') { - c = (char)(c - 'A' + 'a'); - sb.append(c); - } - if (c >= '0' && c <= '9') { - sb.append(c); - } - } - return sb.toString(); - } + s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); - private Boolean checkPalindrome(String target) { - int start = 0; - int end = target.length() - 1; + if (s.length() == 1) return true; - while (start < end) { - if (target.charAt(start) != target.charAt(end)) return false; - start += 1; - end -= 1; + for (int i = 0; i < s.length() / 2; i++) { + if (s.charAt(i) != s.charAt(s.length() - i - 1)) return false; } return true; } From c6ad16ba0abfabce02f0ef21246e4e82f2b42b2c Mon Sep 17 00:00:00 2001 From: ktony Date: Tue, 3 Sep 2024 12:59:21 -0400 Subject: [PATCH 9/9] Valid Palindrome Code refactoring for reducing SC --- valid-palindrome/TonyKim9401.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/valid-palindrome/TonyKim9401.java b/valid-palindrome/TonyKim9401.java index ca747ebb6..e1b13cd17 100644 --- a/valid-palindrome/TonyKim9401.java +++ b/valid-palindrome/TonyKim9401.java @@ -1,14 +1,21 @@ // TC: O(n) -// SC: O(n) +// SC: O(1) class Solution { public boolean isPalindrome(String s) { - s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); + int start = 0; + int end = s.length() - 1; - if (s.length() == 1) return true; + while (start < end) { + while (!Character.isLetterOrDigit(s.charAt(start)) && start < end) start += 1; + while (!Character.isLetterOrDigit(s.charAt(end)) && start < end) end -= 1; - for (int i = 0; i < s.length() / 2; i++) { - if (s.charAt(i) != s.charAt(s.length() - i - 1)) return false; + if (Character.toLowerCase(s.charAt(start)) + != Character.toLowerCase( s.charAt(end))) return false; + + start += 1; + end -= 1; } + return true; } }