Skip to content

Commit e1caac5

Browse files
committed
update 34 java, cheatsheet
1 parent 4085845 commit e1caac5

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

doc/cheatsheet/binary_search.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
- Types
2727
- Basic binary search
28+
- Find start, end idx with target
29+
- LC 34
2830
- Find `LEFT` boundary
2931
- LC 367 Valid Perfect Square
3032
- LC 875 Koko Eating Bananas
@@ -1007,4 +1009,62 @@ public int findMin_3(int[] nums) {
10071009
}
10081010
return Integer.MAX_VALUE;
10091011
}
1012+
```
1013+
1014+
### 2-15) Find First and Last Position of Element in Sorted Array
1015+
1016+
```java
1017+
// java
1018+
// LC 34
1019+
public int[] searchRange_1(int[] nums, int target) {
1020+
int[] result = new int[2];
1021+
result[0] = findFirst(nums, target);
1022+
result[1] = findLast(nums, target);
1023+
return result;
1024+
}
1025+
1026+
private int findFirst(int[] nums, int target) {
1027+
int idx = -1;
1028+
int start = 0;
1029+
int end = nums.length - 1;
1030+
while (start <= end) {
1031+
int mid = (start + end) / 2;
1032+
1033+
/** NOTE !!!
1034+
*
1035+
* 1) nums[mid] >= target (find right boundary)
1036+
* 2) we put equals condition below (nums[mid] == target)
1037+
*/
1038+
if (nums[mid] >= target) {
1039+
end = mid - 1;
1040+
} else {
1041+
start = mid + 1;
1042+
}
1043+
if (nums[mid] == target)
1044+
idx = mid;
1045+
}
1046+
return idx;
1047+
}
1048+
1049+
private int findLast(int[] nums, int target) {
1050+
int idx = -1;
1051+
int start = 0;
1052+
int end = nums.length - 1;
1053+
while (start <= end) {
1054+
int mid = (start + end) / 2;
1055+
/** NOTE !!!
1056+
*
1057+
* 1) nums[mid] <= target (find left boundary)
1058+
* 2) we put equals condition below (nums[mid] == target)
1059+
*/
1060+
if (nums[mid] <= target) {
1061+
start = mid + 1;
1062+
} else {
1063+
end = mid - 1;
1064+
}
1065+
if (nums[mid] == target)
1066+
idx = mid;
1067+
}
1068+
return idx;
1069+
}
10101070
```

leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ private int findFirst(int[] nums, int target) {
6161
int end = nums.length - 1;
6262
while (start <= end) {
6363
int mid = (start + end) / 2;
64+
65+
/** NOTE !!!
66+
*
67+
* 1) nums[mid] >= target (find right boundary)
68+
* 2) we put equals condition below (nums[mid] == target)
69+
*/
6470
if (nums[mid] >= target) {
6571
end = mid - 1;
6672
} else {
@@ -78,6 +84,11 @@ private int findLast(int[] nums, int target) {
7884
int end = nums.length - 1;
7985
while (start <= end) {
8086
int mid = (start + end) / 2;
87+
/** NOTE !!!
88+
*
89+
* 1) nums[mid] <= target (find left boundary)
90+
* 2) we put equals condition below (nums[mid] == target)
91+
*/
8192
if (nums[mid] <= target) {
8293
start = mid + 1;
8394
} else {
@@ -155,4 +166,47 @@ private int binarySearch(int[] nums, int target, boolean isSearchingLeft) {
155166

156167
return idx;
157168
}
169+
170+
// V4
171+
// IDEA : binary Search (gpt)
172+
public int[] searchRange_4(int[] nums, int target) {
173+
if (nums == null || nums.length == 0) {
174+
return new int[]{-1, -1};
175+
}
176+
177+
int start = findBoundary(nums, target, true); // Find the left boundary
178+
if (start == -1) {
179+
return new int[]{-1, -1}; // Target not found
180+
}
181+
182+
int end = findBoundary(nums, target, false); // Find the right boundary
183+
184+
return new int[]{start, end};
185+
}
186+
187+
private int findBoundary(int[] nums, int target, boolean findStart) {
188+
int left = 0;
189+
int right = nums.length - 1;
190+
int boundary = -1;
191+
192+
while (left <= right) {
193+
int mid = left + (right - left) / 2;
194+
195+
if (nums[mid] == target) {
196+
boundary = mid;
197+
if (findStart) {
198+
right = mid - 1; // Narrow down to the left side
199+
} else {
200+
left = mid + 1; // Narrow down to the right side
201+
}
202+
} else if (nums[mid] < target) {
203+
left = mid + 1;
204+
} else {
205+
right = mid - 1;
206+
}
207+
}
208+
209+
return boundary;
210+
}
211+
158212
}

0 commit comments

Comments
 (0)