-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path80_searchMatrix.py
More file actions
27 lines (27 loc) · 984 Bytes
/
80_searchMatrix.py
File metadata and controls
27 lines (27 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''74. Search a 2D Matrix
You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.
Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true'''
from typing import List
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
def binarySearch(left,right,row):
left=0
right=len(matrix[row])-1
while left<=right:
mid=(left+right)//2
if matrix[row][mid]==target:
return True
if matrix[row][mid]>target:
right=mid-1
else:
left=mid+1
return False
for i in range(len(matrix)):
if binarySearch(0,0,i):return True
return False