Skip to content
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
63 changes: 63 additions & 0 deletions Problem_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no

// First, use binary search to find the first position of the target in the array.
// If the target is not found, return [-1, -1].
// If found, use another binary search to find the last position and return both indices.


class Solution {
public int[] searchRange(int[] nums, int target) {


int first = binarySearchFirst(nums, target, 0, nums.length-1);

if(first == -1) return new int[]{-1,-1};

int last = binarySearchLast(nums, target, first, nums.length-1);

return new int[]{first, last};
}

private int binarySearchFirst(int[] nums, int target, int low, int high){

while(low <= high){
int mid = low + (high - low)/2;
if(nums[mid] == target){
if(mid == 0 || nums[mid-1] != target){
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 target, int low, int high){

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

return -1;
}
}
27 changes: 27 additions & 0 deletions Problem_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no


// Use binary search to find the minimum element in a rotated sorted array.
// Compare the middle element with the last element to decide which side to search.
// Keep narrowing the range until low meets high, then return nums[low].


class Solution {
public int findMin(int[] nums) {
int low = 0, high = nums.length - 1;

while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] <= nums[high]) { // right sorted range
high = mid;
} else {
low = mid + 1;
}
}

return nums[low];
}
}
30 changes: 30 additions & 0 deletions Problem_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no

// Use binary search to find a peak element in the array.
// A peak element is greater than its neighbors.
// Move to the side that has a bigger neighbor until a peak is found.


class Solution {
public int findPeakElement(int[] nums) {
int low =0;
int high = nums.length-1;

while(low<=high){
int mid = low+(high-low)/2;

if((mid == 0 || nums[mid-1] < nums[mid]) && (mid == nums.length-1 || nums[mid] > nums[mid+1])){
return mid;
}else if(nums[mid+1] > nums[mid]){
low = mid+1;
}else{
high = mid-1;
}
}
return -1;

}
}