From dbeeda675ac3c630cba2f1b7edd5d77513ba5e1b Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 1 Feb 2025 21:02:38 +0900 Subject: [PATCH 1/4] linked list cycle --- linked-list-cycle/eunhwa99.java | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 linked-list-cycle/eunhwa99.java diff --git a/linked-list-cycle/eunhwa99.java b/linked-list-cycle/eunhwa99.java new file mode 100644 index 000000000..aeae66cd0 --- /dev/null +++ b/linked-list-cycle/eunhwa99.java @@ -0,0 +1,36 @@ + +class ListNode { + + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } +} + +public class Solution { + + // Floyd's Tortoise and Hare Algorithm + // TC: O(N) + // SC: O(1) + public boolean hasCycle(ListNode head) { + if (head == null || head.next == null) { + return false; + } + + 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 60607704b26d523dab215aea2af8afc5a2ff4183 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 1 Feb 2025 21:25:00 +0900 Subject: [PATCH 2/4] find minimum in rotated sorted array --- .../eunhwa99.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/eunhwa99.java diff --git a/find-minimum-in-rotated-sorted-array/eunhwa99.java b/find-minimum-in-rotated-sorted-array/eunhwa99.java new file mode 100644 index 000000000..23d94a5a0 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/eunhwa99.java @@ -0,0 +1,17 @@ +class Solution { + // TC: O(log N) + // SC: O(1) + public int findMin(int[] nums) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left) / 2; + if (nums[mid] < nums[right]) { + right = mid; + } else { + left = mid + 1; + } + } + return nums[left]; + } +} From 97242fbd38fb841f978c4f08915ba82acb82e0e1 Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sat, 1 Feb 2025 22:38:41 +0900 Subject: [PATCH 3/4] maximum product subarray --- maximum-product-subarray/eunhwa99.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 maximum-product-subarray/eunhwa99.java diff --git a/maximum-product-subarray/eunhwa99.java b/maximum-product-subarray/eunhwa99.java new file mode 100644 index 000000000..36bacb7a0 --- /dev/null +++ b/maximum-product-subarray/eunhwa99.java @@ -0,0 +1,26 @@ +class Solution { + + // TP: O(N) + // SP: O(1) + // 음수 원소를 처리하기 위해 곱의 Min 값도 생각해야 했던 문제! + public int maxProduct(int[] nums) { + + int minProd = nums[0]; + int maxProd = nums[0]; + int result = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (nums[i] < 0) { + int temp = minProd; + minProd = maxProd; + maxProd = temp; + } + + maxProd = Math.max(nums[i], maxProd * nums[i]); + minProd = Math.min(nums[i], minProd * nums[i]); + result = Math.max(result, maxProd); + } + + return result; + } +} + From 8ce714bdb9da202fee876f741cec5ceb819b7a3c Mon Sep 17 00:00:00 2001 From: eunhwa99 Date: Sun, 2 Feb 2025 09:39:12 +0900 Subject: [PATCH 4/4] pacific atlantic water flow --- pacific-atlantic-water-flow/eunhwa99.java | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pacific-atlantic-water-flow/eunhwa99.java diff --git a/pacific-atlantic-water-flow/eunhwa99.java b/pacific-atlantic-water-flow/eunhwa99.java new file mode 100644 index 000000000..b4d819c30 --- /dev/null +++ b/pacific-atlantic-water-flow/eunhwa99.java @@ -0,0 +1,63 @@ +import java.util.ArrayList; +import java.util.List; + +class Solution { + + // TP: O(N*N) + // SP: O(N*N) + // pacific에 접하는 지점으로부터 dfs를 시작해 pacific에 도달할 수 있는 지점을 체크하고, + // atlantic에 접하는 지점으로부터 dfs를 시작해 atlantic에 도달할 수 있는 지점을 체크한다. + // 마지막으로 두 지점 모두에 도달할 수 있는 지점을 찾아 반환한다. + int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + + public List> pacificAtlantic(int[][] heights) { + int rowSize = heights.length; + int colSize = heights[0].length; + + boolean[][] pacific = new boolean[rowSize][colSize]; + boolean[][] atlantic = new boolean[rowSize][colSize]; + + for (int i = 0; i < rowSize; i++) { + dfs(i, 0, pacific, heights); + dfs(i, colSize - 1, atlantic, heights); + } + + for (int i = 0; i < colSize; i++) { + dfs(0, i, pacific, heights); + dfs(rowSize - 1, i, atlantic, heights); + } + + List> result = new ArrayList<>(); + for (int i = 0; i < rowSize; i++) { + for (int j = 0; j < colSize; j++) { + if (pacific[i][j] && atlantic[i][j]) { + result.add(List.of(i, j)); + } + } + } + return result; + } + + private void dfs(int row, int col, boolean[][] visited, int[][] heights) { + visited[row][col] = true; + + for (int[] direction : directions) { + int newRow = row + direction[0]; + int newCol = col + direction[1]; + + if (newRow < 0 || newRow >= heights.length || newCol < 0 || newCol >= heights[0].length) { + continue; + } + + if (visited[newRow][newCol]) { + continue; + } + + if (heights[newRow][newCol] < heights[row][col]) { + continue; + } + + dfs(newRow, newCol, visited, heights); + } + } +}