Problem statement

https://binarysearch.com/problems/Matrix-Search-Sequel/

Solution

Equal to Leetcode 0240. Search a 2D Matrix II.

Complexity

It is O(m + n) for time and O(1) for additional space.

Code

class Solution:
    def solve(self, matrix, target):
        x, y = len(matrix[0]) - 1, 0
        while x >= 0 and y < len(matrix):
            if matrix[y][x] > target:
                x -= 1
            elif matrix[y][x] < target:
                y += 1
            else:
                return True
            
        return False