From cb35a960312129d1029b8a7d9e7f7fbaaaca2eff Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Sun, 24 May 2026 18:34:18 -0400 Subject: [PATCH 1/2] Binary Search - 1 --- Problem_1.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem_2.py | 40 ++++++++++++++++++++++++++++++++++++++++ Problem_3.py | 26 ++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 Problem_1.py create mode 100644 Problem_2.py create mode 100644 Problem_3.py diff --git a/Problem_1.py b/Problem_1.py new file mode 100644 index 00000000..98d7b79a --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,51 @@ +''' +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) \ No newline at end of file diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..38891f9b --- /dev/null +++ b/Problem_2.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) \ No newline at end of file diff --git a/Problem_3.py b/Problem_3.py new file mode 100644 index 00000000..6eeb7d28 --- /dev/null +++ b/Problem_3.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 From e29a1e158e6c07225e89c77079359ebdbf57e7e6 Mon Sep 17 00:00:00 2001 From: Kartavya Bhatt Date: Sun, 24 May 2026 21:59:19 -0400 Subject: [PATCH 2/2] Rearranged problems --- Problem_1.py | 67 ++++++++++++++++++++++------------------------------ Problem_2.py | 52 +++++++++++++++------------------------- Problem_3.py | 62 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/Problem_1.py b/Problem_1.py index 98d7b79a..0d7e02b3 100644 --- a/Problem_1.py +++ b/Problem_1.py @@ -1,51 +1,40 @@ ''' -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. +The rotated sorted array will always have one part that is fully sorted. -Then we perform the binary search on the row to find the target and return True if present or False if absent. +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 - def searchRow(self, matrix, target): - l = 0 - r = len(matrix)-1 - - while l <= r: - m = l + (r-l)//2 + m = l + (r-l)//2 - if matrix[m][0] <= target <= matrix[m][-1]: - return m + if nums[m] == target: + return m - if matrix[m][0] > target: - r = m-1 + # 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: - l = m+1 - - return -1 - - def searchCol(self, matrix, row, target): - l = 0 - r = len(matrix[row]) - 1 + return self.search_helper(nums, m+1, r, target) - while l <= r: - m = l + (r-l)//2 - - if matrix[row][m] == target: - return True - - if matrix[row][m] > target: - r = m-1 + # 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: - 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.search_helper(nums, l, m-1, target) + - return self.searchCol(matrix, row, target) \ No newline at end of file + 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 index 38891f9b..6eeb7d28 100644 --- a/Problem_2.py +++ b/Problem_2.py @@ -1,40 +1,26 @@ ''' -The rotated sorted array will always have one part that is fully sorted. +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. -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. +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_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) + 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: - return self.search_helper(nums, l, m-1, target) - + low = mid + 1 - def search(self, nums: List[int], target: int) -> int: - return self.search_helper(nums, 0, len(nums)-1, target) \ No newline at end of file + return -1 diff --git a/Problem_3.py b/Problem_3.py index 6eeb7d28..40d70d86 100644 --- a/Problem_3.py +++ b/Problem_3.py @@ -1,26 +1,52 @@ ''' -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. +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. -Once we get the search window where the target lies, we then do a binary search in the window to get the index. +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 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 + + 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: - low = mid + 1 + 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) +