-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 0 ms (100.00%), Space: 42.9 MB (84.20%) - LeetHub
- Loading branch information
1 parent
823e918
commit 221cf3f
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...find-minimum-in-rotated-sorted-array-ii/0154-find-minimum-in-rotated-sorted-array-ii.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution { | ||
public int findMin(int[] nums) { | ||
// 4 cases | ||
// 1st: left of pivot | ||
// 2nd: right of target | ||
// 3rd: target | ||
// 4th: pivot | ||
int start = 0; | ||
int end = nums.length-1; | ||
// int tempstart = start; | ||
// int tempend = end; | ||
while(start < end){ | ||
int mid = start + (end - start) /2; | ||
int b = mid-1; | ||
int a = mid +1; | ||
b = b < 0? mid:b; | ||
a = a > nums.length-1? mid: a; | ||
|
||
|
||
if (nums[a] < nums[mid]){ | ||
return nums[a]; | ||
} | ||
else if(nums[b] > nums[mid]){ | ||
return nums[mid]; | ||
} | ||
else if(nums[mid] == nums[start] && nums[mid] == nums[end]){ | ||
start++; | ||
end--; | ||
|
||
} | ||
else if(nums[mid] >= nums[start] && !(nums[start] < nums[end])){ | ||
start = mid + 1; | ||
} | ||
else{ | ||
end = mid -1; | ||
} | ||
} | ||
return nums[start]; | ||
|
||
} | ||
} |