Skip to content

Commit

Permalink
Create find-in-matrix.py
Browse files Browse the repository at this point in the history
Finds the presence of a number in a sorted matrix using multiple binary search
  • Loading branch information
gabedonnan authored Jan 27, 2023
1 parent 547dcdf commit 8e33363
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions find-in-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
left = 0
right = len(matrix) - 1
while left <= right:
middle = (left + right) // 2
if matrix[middle][-1] < target:
left = middle + 1
elif matrix[middle][0] > target:
right = middle - 1
else:
return self.binSearch(matrix[middle], target)
return False


def binSearch(self, li: List[int], target: int) -> bool:
left = 0
right = len(li) - 1
while left <= right:
middle = (left + right) // 2
if li[middle] < target:
left = middle + 1
elif li[middle] > target:
right = middle - 1
else:
return True
return False

0 comments on commit 8e33363

Please sign in to comment.