diff --git a/Problem_1.py b/Problem_1.py new file mode 100644 index 00000000..0d7e02b3 --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,40 @@ +''' +The rotated sorted array will always have one part that is fully sorted. + +Finding the mid and then see which side of the mid is sorted. +In the sorted side try to see in the numbers are in the range of target or it contains target by checking the first +and the last item of the side. + +If the target is not in the sorted side, do further binary search in the other side. +If target is in the sorted side, do the further binary search in the sorted side. +''' + +class Solution: + def search_helper(self, nums: List[int], l: int, r: int, target: int): + if l > r: + return -1 + + m = l + (r-l)//2 + + if nums[m] == target: + return m + + # Left of mid is sorted array + if nums[l] <= nums[m]: + # If target lies in the sorted array + if nums[l] <= target < nums[m]: + return self.search_helper(nums, l, m-1, target) + else: + return self.search_helper(nums, m+1, r, target) + + # Right of mid is sorted array + else: + # If target lies in the sorted array + if nums[m] < target <= nums[r]: + return self.search_helper(nums, m+1, r, target) + else: + return self.search_helper(nums, l, m-1, target) + + + def search(self, nums: List[int], target: int) -> int: + return self.search_helper(nums, 0, len(nums)-1, target) diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..6eeb7d28 --- /dev/null +++ b/Problem_2.py @@ -0,0 +1,26 @@ +''' +We start with a search size of 1 and see if the array of search space can contain the target. +If not we move the search window to high and move the high to 2*high meaning increase the search window by +2 times. + +Once we get the search window where the target lies, we then do a binary search in the window to get the index. +''' + +class Solution: + def search(self, reader: 'ArrayReader', target: int) -> int: + low, high = 0, 1 + + while reader.get(high) < target: + low = high + high = high * 2 + + while low <= high: + 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 diff --git a/Problem_3.py b/Problem_3.py new file mode 100644 index 00000000..40d70d86 --- /dev/null +++ b/Problem_3.py @@ -0,0 +1,52 @@ +''' +First we search the first column to find appropriate row. +If the mid row has numbers that can contain the target then we return the mid to do a second searh +to find the target in the row. + +Then we perform the binary search on the row to find the target and return True if present or False if absent. +''' + +class Solution: + + def searchRow(self, matrix, target): + l = 0 + r = len(matrix)-1 + + while l <= r: + m = l + (r-l)//2 + + if matrix[m][0] <= target <= matrix[m][-1]: + return m + + if matrix[m][0] > target: + r = m-1 + else: + l = m+1 + + return -1 + + def searchCol(self, matrix, row, target): + l = 0 + r = len(matrix[row]) - 1 + + while l <= r: + m = l + (r-l)//2 + + if matrix[row][m] == target: + return True + + if matrix[row][m] > target: + r = m-1 + else: + l = m+1 + + return False + + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + row = self.searchRow(matrix, target) + print(row) + if row == -1: + return False + + return self.searchCol(matrix, row, target) +