diff --git a/Problem_1.java b/Problem_1.java new file mode 100644 index 00000000..d911c819 --- /dev/null +++ b/Problem_1.java @@ -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; + } +} \ No newline at end of file diff --git a/Problem_2.java b/Problem_2.java new file mode 100644 index 00000000..432db393 --- /dev/null +++ b/Problem_2.java @@ -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]; + } +} \ No newline at end of file diff --git a/Problem_3.java b/Problem_3.java new file mode 100644 index 00000000..56f5c9ae --- /dev/null +++ b/Problem_3.java @@ -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; + + } +} \ No newline at end of file