-
Notifications
You must be signed in to change notification settings - Fork 40
/
SearchInRotatedSortedArray.java
273 lines (236 loc) · 8.3 KB
/
SearchInRotatedSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package LeetCodeJava.BinarySearch;
// https://leetcode.com/problems/search-in-rotated-sorted-array/
public class SearchInRotatedSortedArray {
// V0
// IDEA : BINARY SEARCH
// CASE 1) sub array left is sorted
// CASE 2) sub array right is sorted
// https://www.youtube.com/watch?v=U8XENwh8Oy8
public int search(int[] nums, int target) {
if (nums.length == 0 || nums.equals(null)){
return -1;
}
int l = 0;
int r = nums.length - 1;
while (r >= l){
int mid = (l + r) / 2;
int cur = nums[mid];
if (cur == target){
return mid;
}
// else if (cur > nums[0] && target > cur){
// //l = mid + 1;
// nums_sub = Arrays.copyOfRange(nums, mid+1, nums.length-1);
// }else if (cur > nums[0] && target < cur){
// //r = mid - 1;
// nums_sub = Arrays.copyOfRange(nums, l, mid-1);
// }
// else if (cur < nums[0] && target > cur){
// //r = mid - 1;
// nums_sub = Arrays.copyOfRange(nums, mid+1, nums.length-1);
// }else{
// nums_sub = Arrays.copyOfRange(nums, l, mid-1);
// }
//
// return _binarySearch(nums_sub, target);
/** NOTE !!! we use below logic for dealing with
* cases such as
* - 1) left sub array is sorted
* - nums[mid] >= nums[l] && target < nums[mid]
* - nums[mid] < nums[l] || target > nums[mid]
* - 2) right sub array is sorted
* - target <= nums[r] && target > nums[mid]
* - target > nums[r] || target < nums[mid]
*
*/
// Case 1: subarray on mid's left is sorted
/** NOTE !!! we compare mid with left, instead of 0 idx element */
else if (nums[mid] >= nums[l]) {
if (target >= nums[l] && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
}
// Case 2: subarray on mid's right is sorted
else {
if (target <= nums[r] && target > nums[mid]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
// V0'
// IDEA : BINARY SEARCH (fixed by GPT)
public int search_0(int[] nums, int target) {
if (nums.length == 0){
return -1;
}
int l = 0;
int r = nums.length - 1;
// NOTE : "r >= l" as binary search condition
while (r >= l){
int mid = (l + r) / 2;
int cur = nums[mid];
// case 1)
if (cur == target){
return mid;
}
/**
* case 2) mid is pivot, left half is sorted.
* left array is sorted (increasing)
*/
else if (nums[mid] >= nums[l]){
/**
* NOTE !!!
*
* since left sub array is sorted (increasing)
* the only condition we can use is : left sub array
* e.g. : check if target is bigger than left boundary or not
*/
/**
*
* NOTE !!!
* below is WRONG!!! (right sub array may have "pivot",
* can't use right sub array as condition
*/
// if (target > nums[mid] && nums[r] >= target){
// l = mid + 1;
// }else{
// r = mid - 1; // NOTE !!!! not "r = mid"
// }
if (target >= nums[l] && target < cur) {
r = mid - 1;
} else {
l = mid + 1;
}
}
/**
* Case 3): right half is sorted (increasing)
*/
/**
* NOTE !!!
*
* since right sub array is sorted (increasing)
* the only condition we can use is : right sub array
* e.g. : check if target is smaller than right boundary or not
*/
else{
if (target > nums[mid] && target <= nums[r]){
l = mid + 1;
}else{
r = mid - 1; // NOTE !!! not " r = mid"
}
}
}
return -1;
}
// V1
// IDEA : Find Pivot Index + Binary Search
// https://leetcode.com/problems/search-in-rotated-sorted-array/editorial/
public int search_2(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1;
// Find the index of the pivot element (the smallest element)
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > nums[n - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// Binary search over elements on the pivot element's left
int answer = binarySearch(nums, 0, left - 1, target);
if (answer != -1) {
return answer;
}
// Binary search over elements on the pivot element's right
return binarySearch(nums, left, n - 1, target);
}
// Binary search over an inclusive range [left_boundary ~ right_boundary]
private int binarySearch(int[] nums, int leftBoundary, int rightBoundary, int target) {
int left = leftBoundary, right = rightBoundary;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
// V2
// IDEA : Find Pivot Index + Binary Search with Shift
// https://leetcode.com/problems/search-in-rotated-sorted-array/editorial/
public int search_3(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1;
// Find the index of the pivot element (the smallest element)
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > nums[n - 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return shiftedBinarySearch(nums, left, target);
}
// Shift elements in a circular manner, with the pivot element at index 0.
// Then perform a regular binary search
private int shiftedBinarySearch(int[] nums, int pivot, int target) {
int n = nums.length;
int shift = n - pivot;
int left = (pivot + shift) % n;
int right = (pivot - 1 + shift) % n;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[(mid - shift + n) % n] == target) {
return (mid - shift + n) % n;
} else if (nums[(mid - shift + n) % n] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
// V3
// IDEA : One Binary Search
// https://leetcode.com/problems/search-in-rotated-sorted-array/editorial/
public int search_4(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Case 1: find target
if (nums[mid] == target) {
return mid;
}
// Case 2: subarray on mid's left is sorted
else if (nums[mid] >= nums[left]) {
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
// Case 3: subarray on mid's right is sorted
else {
if (target <= nums[right] && target > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}