From e1caac54f101f53022a6d8ca16793e1967806b3c Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 30 Nov 2024 15:43:20 +0800 Subject: [PATCH] update 34 java, cheatsheet --- doc/cheatsheet/binary_search.md | 60 +++++++++++++++++++ ...AndLastPositionOfElementInSortedArray.java | 54 +++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/doc/cheatsheet/binary_search.md b/doc/cheatsheet/binary_search.md index 85517113..edc52be4 100644 --- a/doc/cheatsheet/binary_search.md +++ b/doc/cheatsheet/binary_search.md @@ -25,6 +25,8 @@ - Types - Basic binary search + - Find start, end idx with target + - LC 34 - Find `LEFT` boundary - LC 367 Valid Perfect Square - LC 875 Koko Eating Bananas @@ -1007,4 +1009,62 @@ public int findMin_3(int[] nums) { } return Integer.MAX_VALUE; } +``` + +### 2-15) Find First and Last Position of Element in Sorted Array + +```java +// java +// LC 34 + public int[] searchRange_1(int[] nums, int target) { + int[] result = new int[2]; + result[0] = findFirst(nums, target); + result[1] = findLast(nums, target); + return result; + } + + private int findFirst(int[] nums, int target) { + int idx = -1; + int start = 0; + int end = nums.length - 1; + while (start <= end) { + int mid = (start + end) / 2; + + /** NOTE !!! + * + * 1) nums[mid] >= target (find right boundary) + * 2) we put equals condition below (nums[mid] == target) + */ + if (nums[mid] >= target) { + end = mid - 1; + } else { + start = mid + 1; + } + if (nums[mid] == target) + idx = mid; + } + return idx; + } + + private int findLast(int[] nums, int target) { + int idx = -1; + int start = 0; + int end = nums.length - 1; + while (start <= end) { + int mid = (start + end) / 2; + /** NOTE !!! + * + * 1) nums[mid] <= target (find left boundary) + * 2) we put equals condition below (nums[mid] == target) + */ + if (nums[mid] <= target) { + start = mid + 1; + } else { + end = mid - 1; + } + if (nums[mid] == target) + idx = mid; + } + return idx; + } ``` \ No newline at end of file diff --git a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java index 2930ec22..29b9f1ad 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java @@ -61,6 +61,12 @@ private int findFirst(int[] nums, int target) { int end = nums.length - 1; while (start <= end) { int mid = (start + end) / 2; + + /** NOTE !!! + * + * 1) nums[mid] >= target (find right boundary) + * 2) we put equals condition below (nums[mid] == target) + */ if (nums[mid] >= target) { end = mid - 1; } else { @@ -78,6 +84,11 @@ private int findLast(int[] nums, int target) { int end = nums.length - 1; while (start <= end) { int mid = (start + end) / 2; + /** NOTE !!! + * + * 1) nums[mid] <= target (find left boundary) + * 2) we put equals condition below (nums[mid] == target) + */ if (nums[mid] <= target) { start = mid + 1; } else { @@ -155,4 +166,47 @@ private int binarySearch(int[] nums, int target, boolean isSearchingLeft) { return idx; } + + // V4 + // IDEA : binary Search (gpt) + public int[] searchRange_4(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return new int[]{-1, -1}; + } + + int start = findBoundary(nums, target, true); // Find the left boundary + if (start == -1) { + return new int[]{-1, -1}; // Target not found + } + + int end = findBoundary(nums, target, false); // Find the right boundary + + return new int[]{start, end}; + } + + private int findBoundary(int[] nums, int target, boolean findStart) { + int left = 0; + int right = nums.length - 1; + int boundary = -1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (nums[mid] == target) { + boundary = mid; + if (findStart) { + right = mid - 1; // Narrow down to the left side + } else { + left = mid + 1; // Narrow down to the right side + } + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return boundary; + } + }