From 6ebd1db3172208a917f36bea27d2e90f2c82ab91 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 4 Feb 2025 23:15:46 +0900 Subject: [PATCH 1/5] feat : find-minimum-in-rotated-sorted-array --- .../ekgns33.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/ekgns33.java diff --git a/find-minimum-in-rotated-sorted-array/ekgns33.java b/find-minimum-in-rotated-sorted-array/ekgns33.java new file mode 100644 index 000000000..d81080909 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/ekgns33.java @@ -0,0 +1,56 @@ +/** + input : array of integers + output : minimum element's value + + 3 4 5 1 2 + draw graph + + 5 + 4 + 3 + 2 + 1 + l r + l r + l r + -------------- + 5 + 4 + 3 + 2 + 1 + mid right + -------------- + left < right ->> sorted. + left < mid ->> don't have to search range [left, mid] + mid > right ->> rotation point is between [mid, right]; + + solution 1) brute force + read the array + + tc : O(n) + sc : O(1); + + solution 2) binary search + do binary search, move pointers with descripted conditions + after binary search loop ends, the left pointer is the minimum element + + tc : O(logn) + sc : O(1) + */ + +class Solution { + public int findMin(int[] nums) { + int l = 0; + int r = nums.length - 1; + while(l < r) { + int mid = (r - l) / 2 + l; + if(nums[mid] < nums[r]) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l]; + } +} From e2977c41551ef3714f5e2ac0bb87d248be283764 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Tue, 4 Feb 2025 23:16:00 +0900 Subject: [PATCH 2/5] feat : liked-list-cycle --- linked-list-cycle/ekgns33.java | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 linked-list-cycle/ekgns33.java diff --git a/linked-list-cycle/ekgns33.java b/linked-list-cycle/ekgns33.java new file mode 100644 index 000000000..8e9aa79c7 --- /dev/null +++ b/linked-list-cycle/ekgns33.java @@ -0,0 +1,35 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ + +/* +* input : singly linked list and head node +* output : return true if list has cycle +* +* solution : tortoise and hare algorithm +* with two pointer (slow and fast) +* tc : O(n) +* sc : O(1) +* +* */ +public class Solution { + public boolean hasCycle(ListNode head) { + if(head == null) return false; + ListNode slow = head; + ListNode fast = head; + while(fast.next != null && fast.next.next != null) { + slow = slow.next; + fast = fast.next.next; + if(fast == slow) return true; + } + return false; + } +} From d991815e7597af22aefbcf1bdcda7889957793d4 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Thu, 6 Feb 2025 23:55:48 +0900 Subject: [PATCH 3/5] feat : pacific-atlantic-water-flow --- pacific-atlantic-water-flow/ekgns33.java | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pacific-atlantic-water-flow/ekgns33.java diff --git a/pacific-atlantic-water-flow/ekgns33.java b/pacific-atlantic-water-flow/ekgns33.java new file mode 100644 index 000000000..2f3975576 --- /dev/null +++ b/pacific-atlantic-water-flow/ekgns33.java @@ -0,0 +1,62 @@ +/* +* input : grid represents the heights of island +* output : coordinates where water flows to both pacific and atlantic +* +* example +* +* 2 2 2 1,1 >> 1,2 >> atlantic / 1,1 >> 0,1 >> pacific +* 3 3 1 +* 4 3 1 +* +* do dfs/bfs from the edge of grids. +* if cell is checked from pacific and atlantic add to result +* solution dfs from edges +* tc : O(m*n) +* sc : O(mn) +* +* */ +class Solution { + public List> pacificAtlantic(int[][] heights) { + int m = heights.length; + int n = heights[0].length; + int[][] pacific = new int[m][n]; + int[][] atlantic = new int[m][n]; + + for(int i = 0; i < m; i++) { + dfsHelper(heights, pacific, i, 0); + } + for(int j = 1; j < n; j++) { + dfsHelper(heights,pacific, 0, j); + } + for(int i =0; i < m; i++) { + dfsHelper(heights,atlantic, i, n-1); + } + for(int j = 0; j < n-1; j++) { + dfsHelper(heights, atlantic, m-1, j); + } + List> ans = new ArrayList<>(); + for(int i = 0; i < m; i++) { + for(int j = 0; j < n; j++) { + if(pacific[i][j] == 1 && atlantic[i][j] == 1) { + ans.add(List.of(i, j)); + } + } + } + return ans; + } + + static int[][] directions = { + {1, 0}, {-1, 0}, {0, 1}, {0, -1} + }; + + void dfsHelper(int[][] board, int[][] visited, int x, int y) { + if(visited[x][y] > 0) return; + visited[x][y] += 1; + for(int[] direction : directions) { + int nx = x + direction[0]; + int ny = y + direction[1]; + if(nx < 0 || nx >=visited.length || ny < 0 || ny >= visited[0].length || visited[nx][ny] > 0 || board[nx][ny] < board[x][y]) continue; + dfsHelper(board, visited, nx, ny); + } + } +} From cf5f6c2d1382e10447f1c2173f6636e354f05c2d Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 7 Feb 2025 00:16:31 +0900 Subject: [PATCH 4/5] feat : maximum subarray product --- maximum-product-subarray/ekgns33.java | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 maximum-product-subarray/ekgns33.java diff --git a/maximum-product-subarray/ekgns33.java b/maximum-product-subarray/ekgns33.java new file mode 100644 index 000000000..bc101bfc3 --- /dev/null +++ b/maximum-product-subarray/ekgns33.java @@ -0,0 +1,45 @@ +class Solution { + public int maxProduct(int[] nums) { + int n = nums.length; + int[][] product = new int[2][n]; + product[0][0] = nums[0]; + product[1][0] = nums[0]; + int max = nums[0]; + for(int i = 1; i < n; i++) { + product[0][i] = Math.max(product[0][i-1]*nums[i], Math.max(nums[i], nums[i] * product[1][i-1])); + product[1][i] = Math.min(product[0][i-1]*nums[i], Math.min(nums[i], nums[i] * product[1][i-1])); + max =Math.max(max, product[0][i]); + } + return max; + } +} +/** + + + brute force : + nested for loop + tc : O(n^2) + sc : O(1) + + better sol : + maintain min and max + tc : O(n) + sc : O(n) + + compare with prev Min * cur, prevMax * cur, cur + we have to keep track of minimum value that can lead to maximum value + when there are negative values later. + + 2 3 -2 4 + 2 6 -2 4 + 2 2 -12 -48 + + -2 0 -1 + -2 0 0 + -2 0 -1 + + 2 3 -2 5 7 -100 + 2 6 -2 5 35 210000 + 2 3 -6 -30 -210. -35 + + */ From fddbef525bdf1ba2b6e3d5941cf6a1ae68cb49e7 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Sat, 8 Feb 2025 01:57:31 +0900 Subject: [PATCH 5/5] feat : minimum-window-substring --- minimum-window-substring/ekgns33.java | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 minimum-window-substring/ekgns33.java diff --git a/minimum-window-substring/ekgns33.java b/minimum-window-substring/ekgns33.java new file mode 100644 index 000000000..2557f8ef9 --- /dev/null +++ b/minimum-window-substring/ekgns33.java @@ -0,0 +1,30 @@ +class Solution { + public String minWindow(String s, String t) { + int[] freq = new int[128]; + char[] str = s.toCharArray(); + int minL = 0; + int minLength = Integer.MAX_VALUE; + int l = 0, r = 0, n = s.length(); + for(char c : t.toCharArray()) { + freq[c-'A']++; + } + int resolved = t.length(); + while(r < n) { + if(freq[str[r++] - 'A']-- > 0) { + resolved--; + } + while(resolved == 0) { + if(r - l < minLength) { + minL = l; + minLength = r - l; + } + if(freq[str[l] - 'A']++ == 0) { + resolved++; + } + l++; + } + } + if(minLength == Integer.MAX_VALUE) return ""; + return new String(str, minL, minLength); + } +}