Skip to content

Commit

Permalink
update 34 java, cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 30, 2024
1 parent 4085845 commit e1caac5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
60 changes: 60 additions & 0 deletions doc/cheatsheet/binary_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

}

0 comments on commit e1caac5

Please sign in to comment.