From 78bec4d793cb6ab45eacef50e3f9819047f79b39 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 6 Oct 2024 18:08:47 -0400 Subject: [PATCH 1/5] Linked List Cycle --- linked-list-cycle/TonyKim9401.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 linked-list-cycle/TonyKim9401.java diff --git a/linked-list-cycle/TonyKim9401.java b/linked-list-cycle/TonyKim9401.java new file mode 100644 index 000000000..d65c11575 --- /dev/null +++ b/linked-list-cycle/TonyKim9401.java @@ -0,0 +1,15 @@ +// TC: O(n) +// SC: O(1) +public class Solution { + public boolean hasCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + if (slow == fast) return true; + } + return false; + } +} From 315365d6bcbeb3a6b9ca1e173f1b127276bd8e40 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 9 Oct 2024 21:20:39 -0400 Subject: [PATCH 2/5] Find Minimum In Rotated Sorted Array --- .../TonyKim9401.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/TonyKim9401.java diff --git a/find-minimum-in-rotated-sorted-array/TonyKim9401.java b/find-minimum-in-rotated-sorted-array/TonyKim9401.java new file mode 100644 index 000000000..6a78863f0 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/TonyKim9401.java @@ -0,0 +1,25 @@ +// TC: O(log n) +// Using binary search, it takes `log n` time complexity, n indicates the length of the given array nums +// SC: O(1) +// constant space occupation +class Solution { + public int findMin(int[] nums) { + + int start = 0; + int end = nums.length - 1; + int min = Integer.MAX_VALUE; + + while (start <= end) { + int mid = start + (end - start) / 2; + + if (nums[start] <= nums[mid]) { + min = Math.min(min, nums[start]); + start = mid + 1; + } else if (nums[mid] <= nums[end]) { + min = Math.min(min, nums[mid]); + end = mid - 1; + } + } + return min; + } +} From 69030c04fd20a1ed65db497e04167d741a710b59 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 9 Oct 2024 21:23:15 -0400 Subject: [PATCH 3/5] Pacific Atlantic Water Flow --- pacific-atlantic-water-flow/TonyKim9401.java | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pacific-atlantic-water-flow/TonyKim9401.java diff --git a/pacific-atlantic-water-flow/TonyKim9401.java b/pacific-atlantic-water-flow/TonyKim9401.java new file mode 100644 index 000000000..2801aea66 --- /dev/null +++ b/pacific-atlantic-water-flow/TonyKim9401.java @@ -0,0 +1,44 @@ +// TC: O(n * m) +// visit all elements +// SC: O(n * m) +// create result from all elements +class Solution { + public List> pacificAtlantic(int[][] heights) { + List> output = new ArrayList<>(); + + if (heights.length == 0 || heights[0].length == 0) return output; + + int rows = heights.length; + int cols = heights[0].length; + + boolean[][] pac = new boolean[rows][cols]; + boolean[][] atl = new boolean[rows][cols]; + + for (int j = 0; j < rows; j++) { + dfs(j, 0, pac, heights[j][0], heights); + dfs(j, cols-1, atl, heights[j][cols-1], heights); + } + + for (int i = 0; i < cols; i++) { + dfs(0, i, pac, heights[0][i], heights); + dfs(rows-1, i, atl, heights[rows-1][i], heights); + } + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (pac[i][j] && atl[i][j]) output.add(List.of(i,j)); + } + } + return output; + } + + private void dfs(int i, int j, boolean[][] visit, int preValue, int[][] heights) { + if (i < 0 || j < 0 || i == heights.length || j == heights[0].length || visit[i][j] || preValue > heights[i][j]) return; + + visit[i][j] = true; + dfs(i + 1, j, visit, heights[i][j], heights); + dfs(i - 1, j, visit, heights[i][j], heights); + dfs(i, j + 1, visit, heights[i][j], heights); + dfs(i, j - 1, visit, heights[i][j], heights); + } +} From 6d4765e164884241ded4f985c3ed6828ad98ec47 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 9 Oct 2024 21:24:27 -0400 Subject: [PATCH 4/5] Maximum Subarray --- maximum-subarray/TonyKim9401.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 maximum-subarray/TonyKim9401.java diff --git a/maximum-subarray/TonyKim9401.java b/maximum-subarray/TonyKim9401.java new file mode 100644 index 000000000..55bd93ad2 --- /dev/null +++ b/maximum-subarray/TonyKim9401.java @@ -0,0 +1,17 @@ +// TC: O(n) +// visit all elements once for each +// SC: O(1) +// constant space occupation +class Solution { + public int maxSubArray(int[] nums) { + int total = 0; + int output = nums[0]; + + for (int num : nums) { + if (total < 0) total = 0; + total += num; + output = total > output ? total : output; + } + return output; + } +} From 89cce77372d57c69288c9f738c14cf592abe1dd0 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 10 Oct 2024 13:10:23 -0400 Subject: [PATCH 5/5] Minimum Window Substring --- minimum-window-substring/TonyKim9401.java | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 minimum-window-substring/TonyKim9401.java diff --git a/minimum-window-substring/TonyKim9401.java b/minimum-window-substring/TonyKim9401.java new file mode 100644 index 000000000..0759effbd --- /dev/null +++ b/minimum-window-substring/TonyKim9401.java @@ -0,0 +1,52 @@ +// TC: O(n) +// using two pointer lef and right, it visits all elements only once each. +// SC: O(n + m) +// 2 hashmap used for checking the given Strings s and t, n is the size of s, m is the size of m +class Solution { + public String minWindow(String s, String t) { + Map map = new HashMap<>(); + + for (char c : t.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + + int required = map.size(); + int formed = 0; + + int left = 0; + int right = 0; + int[] ans = {-1, 0, 0}; + + Map windowCounts = new HashMap<>(); + + while (right < s.length()) { + char c = s.charAt(right); + windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1); + + if (map.containsKey(c) && + windowCounts.get(c).intValue() == map.get(c).intValue()) { + formed += 1; + } + + while (left <= right && formed == required) { + c = s.charAt(left); + + if (ans[0] == -1 || right - left + 1 < ans[0]) { + ans[0] = right - left + 1; + ans[1] = left; + ans[2] = right; + } + + windowCounts.put(c, windowCounts.get(c) - 1); + if (map.containsKey(c) && + windowCounts.get(c).intValue() < map.get(c).intValue()) { + formed -= 1; + } + + left += 1; + } + right += 1; + } + return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1); + } +}