Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done Binary Search 2 #2037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions FirstAndLastInSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
class Solution {
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
int low = 0;
int high = n - 1;
if (nums == null || n == 0)
return new int[] { -1, -1 };
if (target < nums[0] || target > nums[n - 1])
return new int[] { -1, -1 };
int firstIndex = binarySearchFirst(nums, low, high, target);
if (firstIndex == -1)
return new int[] { -1, -1 };
int lastIndex = binarySearchLast(nums, firstIndex, high, target);
return new int[] { firstIndex, lastIndex };
}

private int binarySearchFirst(int[] nums, int low, int high, int target) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
if (mid == 0 || nums[mid] != nums[mid - 1]) {
return mid;
} else {
high = mid - 1;
}
} else if (nums[mid] > target) {
high = mid - 1;

} else {
low = mid + 1;
}
}
return -1;
}

private int binarySearchLast(int[] nums, int low, int high, int target) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
if (mid == high || nums[mid] != nums[mid + 1]) {
return mid;
} else {
low = mid + 1;
}
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}
26 changes: 26 additions & 0 deletions MinInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
class Solution {
public int findMin(int[] nums) {
int n = nums.length;
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[low] <= nums[high]) { // checking sorted array
return nums[low];
} else if ((mid == 0 || nums[mid] < nums[mid - 1]) && (nums[mid] < nums[mid + 1])) {
return nums[mid]; // checking left and right of mid to determine if mid is min;
} else if (nums[low] <= nums[mid]) { // move the search to unsorted range;

low = mid + 1;
} else {
high = mid - 1;
}

}
return 8585;
}
}
23 changes: 23 additions & 0 deletions PeakElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if ((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == n - 1 || nums[mid] > nums[mid + 1])) {
return mid;
} else if (mid > 0 && nums[mid - 1] > nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}

}
return 8585;
}
}