class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if len(matrix) == 0 or len(matrix[0]) == 0:
return False
height = len(matrix)
width = len(matrix[0])
row = height - 1
col = 0
while col < width and row >= 0:
if matrix[row][col] > target:
row -= 1
elif matrix[row][col] < target:
col += 1
else:
return True
return False
Any improvements you could make to this? Runtime is O(Len(row) + len(col)), space is constant.
I looked at other solutions and a lot of them used binary search, but that is O(len(col) * log(len(row)) so I don't think that's an improvement.
O(n) > O(log(n))\$\endgroup\$O(n + k) < O(n log k)for the most part. \$\endgroup\$O(log(n) + log(m))orlog(n + m)\$\endgroup\$