diff --git a/SearchIn2dMatrix.java b/SearchIn2dMatrix.java new file mode 100644 index 00000000..8afb297e --- /dev/null +++ b/SearchIn2dMatrix.java @@ -0,0 +1,29 @@ +// Time Complexity : O(log m*n) as it's binary search on the complete matrix size. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Not much, once the idea to consider it as a single array and perform the binary search to achieve O(log m*n) it was +// better. The core part of the solution lies in identifying the position of mid element. + +// Your code here along with comments explaining your approach + + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int low = 0; + int high = (matrix.length * matrix[0].length) - 1; + while(low <= high){ + int mid = low + (high-low)/2; + int column = mid%matrix[0].length; + int row = mid/matrix[0].length; + if(matrix[row][column] == target){ + return true; + } + if(matrix[row][column] > target){ + high = mid - 1; + } else{ + low = mid + 1; + } + } + return false; + } +} \ No newline at end of file diff --git a/SearchInArrayReader.java b/SearchInArrayReader.java new file mode 100644 index 00000000..71217a83 --- /dev/null +++ b/SearchInArrayReader.java @@ -0,0 +1,30 @@ +// Time Complexity : O(log m) - m is the position of the target. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Not much, once the idea to find the search place is cracked, it was a straight binary search on the space. + +// Your code here along with comments explaining your approach + + +class Solution { + public int search(ArrayReader reader, int target) { + int low = 0, high = 1; + + while(reader.get(high) < target){ + low = high ; + high = high * 2; + } + //binary search + while(low <= high){ + int mid = low + (high - low)/2; + if(reader.get(mid) == target) return mid; + if(reader.get(mid) > target){ + high = mid - 1; + }else{ + low = mid + 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/SearchInRotatedArray.java b/SearchInRotatedArray.java new file mode 100644 index 00000000..ac4bbb53 --- /dev/null +++ b/SearchInRotatedArray.java @@ -0,0 +1,38 @@ +// Time Complexity : O(log n) as it's binary search. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Not much, once the idea to find the sorted part on the array, it is a binary search to find the target. + +// Your code here along with comments explaining your approach + + +class Solution { + public int search(int[] nums, int target) { + int low = 0; + int high = nums.length - 1; + int mid = 0; + + while(low<=high){ + mid = low + (high-low)/2; //to avoid number overflow + if(nums[mid] == target){ + return mid; + } + if(nums[low] <= nums[mid]){ // considering the left half + if(nums[low] <= target && nums[mid] > target){ //checking if target is in left half + high = mid - 1; + } + else{ + low = mid + 1; + } + }else{ // considering the right half + if(nums[mid] < target && nums[high] >= target){ + low = mid + 1; + }else{ + high = mid - 1; + } + } + } + + return -1; + } +} \ No newline at end of file