Topics
Idea
Straightforward application of the staircase algorithm, leveraging the sorted nature of the matrix.
Algorithm
- Start at Top-Right: Begin at the top-right element of the matrix (
row = 0
,col = n - 1
). - Comparison and Movement:
- If
matrix[row][col] == target
: You’ve found the target; returnTrue
. - If
matrix[row][col] > target
: Move one column to the left (col -= 1
). Because the row is sorted, all elements to the right are greater than the target. - If
matrix[row][col] < target
: Move one row down (row += 1
). Because the column is sorted, all elements above are smaller than the target.
- If
- Out of Bounds: If at any point
row
goes out of bounds (becomes>= m
) orcol
goes out of bounds (becomes< 0
), the target is not in the matrix; returnFalse
.
Code
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
n, m = len(matrix), len(matrix[0])
i, j = 0, m-1
while (0 <= i < n) and (0 <= j < m):
curr = matrix[i][j]
if (curr == target): return True
elif (curr < target): i += 1
else: j -= 1
return False