diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..c356e433 --- /dev/null +++ b/problem1.java @@ -0,0 +1,30 @@ +// Time Complexity : O(log(m*n)) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + if (matrix == null || matrix.length == 0) { + return false; + } + int m = matrix.length; + int n = matrix[0].length; + int low = 0; + int high = m*n-1; + while (low <= high) { + int mid = low + (high - low) / 2; + int row = mid / n; + int col = mid % n; + if (matrix[row][col] == target) { + return true; + } + else if (matrix[row][col] > target) { + high = mid - 1; + } + else { + low = mid + 1; + } + } + return false; + } +} \ No newline at end of file diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..352e5839 --- /dev/null +++ b/problem1.py @@ -0,0 +1,24 @@ +# Time Complexity : O(log(m*n)) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + if matrix == None or len(matrix) == 0: + return False + m = len(matrix) + n = len(matrix[0]) + low = 0 + high = m*n-1 + while low <= high: + mid = low + (high - low) // 2 + row = mid // n + col = mid % n + print("row = ", row , "col = ",col) + if matrix[row][col] == target: + return True + elif matrix[row][col] > target: + high = mid - 1 + else: + low = mid + 1 + return False \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..e89057f1 --- /dev/null +++ b/problem2.java @@ -0,0 +1,40 @@ +// 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 +class Solution { + public int search(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return -1; + } + int n = nums.length; + int low = 0; + int high = n - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; //prevent integer overflow + + if (target == nums[mid]) { + return mid; + } + else if (nums[low] <= nums[mid]) { + if (target >= nums[low] && target <= nums[mid]){ + high = mid -1; + } + else { + low = mid + 1; + } + } + else { + if (target > nums[mid] && target <= nums[high]) { + low = mid + 1; + } + else { + high = mid - 1; + } + } + + } + return -1; + } +} \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..dcae727a --- /dev/null +++ b/problem2.py @@ -0,0 +1,28 @@ +# 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 +class Solution: + def search(self, nums: List[int], target: int) -> int: + if nums == None and len(nums) == 0: + return -1 + n = len(nums) + low = 0 + high = n-1 + + while low <= high: + mid = low + (high - low) // 2 + + if target == nums[mid]: + return mid; + elif nums[low] <= nums[mid]: + if target >= nums[low] and target <= nums[mid]: + high = mid-1 + else: + low = mid+1 + else: + if target > nums[mid] and target <= nums[high]: + low = mid+1 + else: + high = mid-1 + return -1 \ No newline at end of file diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..c6cf8031 --- /dev/null +++ b/problem3.java @@ -0,0 +1,37 @@ +// 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 +class Solution { + public int search (ArrayReader reader, int target) { + int low = 0; + int high = 1; + + while (reader.get(high) < target) { + low = high; + high = high * 2; + } + + if (reader.get(high) == target) { + return high; + } + + return binary_search(reader, low, high, target); + } + + public int binary_search(ArrayReader reader, int low, int high, int target) { + while (low <= high) { + int mid = low + (high - low) / 2; + + if (reader.get(mid) == target) { + return mid; + } else if (reader.get(mid) < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..dae6f380 --- /dev/null +++ b/problem3.py @@ -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 +class Solution: + def search(self, reader: 'ArrayReader', target: int) -> int: + low = 0 + high = 1 + + while reader.get(high) < target: + low = high + high = high * 2 + if reader.get(high) == target: + return high + + return self.binary_search(reader, low, high, target) + + def binary_search(self, reader: 'ArrayReader', low: int, high: int, target: int) -> int: + while low <= high: + mid = low + (high - low) // 2 + if reader.get(mid) == target: + return mid + elif reader.get(mid) < target: + low = mid + 1 + else: + high = mid - 1 + return -1 \ No newline at end of file