-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finds the presence of a number in a sorted matrix using multiple binary search
- Loading branch information
1 parent
547dcdf
commit 8e33363
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |