diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..9cd876db --- /dev/null +++ b/Problem1.py @@ -0,0 +1,42 @@ +# 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 +# Three line explanation of solution in plain english: +#1. We use binary search to find the target in a 2D matrix for this we flatten the 2d matrix to an array. +#2. We treat the 2D matrix as a 1D array by calculating the row and column indices from the mid index. +#we use r and c by dividing mid by n (number of columns) and taking the modulus with n respectively. +#3. If the target is found, we return True; otherwise, we adjust the search range based on the comparison with the mid element. + +class Solution(object): + def searchMatrix(self, matrix, target): + """ + :type matrix: List[List[int]] + :type target: int + :rtype: bool + """ + #get the number of rows and columns in the matrix + m=len(matrix) + n=len(matrix[0]) + #low will be the starting index and high will be the last index of the flattened matrix + low=0 + high=m*n-1 + #binary search to find the target in the flattened matrix + while low<=high: + mid=low+(high-low)/2 + #calculate the row and column indices from the mid index + #if you divide mid by number of columns n, you get the row index r + #and if you take mid modulo n, you get the column index c + r=mid/n + c=mid%n + #if the target is found at the mid position, return True + if matrix[r][c]==target: + return True + #if the target is less than the mid element, adjust the high pointer to mid-1 and search in the left half + # if the target is greater than the mid element, adjust the low pointer to mid+1 and search in the right half + if matrix[r][c]target: + high=mid-1 + else: + low=mid+1 + #right side sorted + else: + #if it does, increase the low pointer to mid+1, else reduce the high pointer to mid + if nums[mid]=target: + low=mid+1 + else: + high=mid-1 + #if the target is not found, return -1 + return -1 diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..39f5237f --- /dev/null +++ b/Problem3.py @@ -0,0 +1,39 @@ +# Time Complexity : O(log n) +# Space Complexity : O(1) +#Did this code successfully run on Leetcode : No +#Any problem you faced while coding this : No +# Three line explanation of solution in plain english: +# 1. The solution uses an exponential search to find the range where the target might be located. +# 2. It then applies binary search within that range to find the exact index of the target. +# 3. The reader.get method is used to access elements, simulating a scenario where the array size is unknown. + + +class Solution: + def search(self, reader, target): + low = 0 + high = 1 + + # Expand the search range exponentially + # until the value at high index is greater than or equal to the target + while reader.get(high) < target: + low = high + high *= 2 + + return self.binarySearch(reader, target, low, high) + # + # Perform binary search within the determined range + def binarySearch(self, reader, target, low, high): + while low <= high: + mid = low + (high - low) // 2 + # Use reader.get to access the value at mid + val = reader.get(mid) + + # Check if the value at mid is equal to the target + if val == target: + return mid + elif val > target: + high = mid - 1 + else: + low = mid + 1 + # If the target is not found, return -1 + return -1