CT925

The Sorted Matrix: Search in a Row-Column Sorted Matrix

MediumL6 · Pointer PhantomAcceptance: 0.0%XP: 100

In the crystal palace of Prime City, the Royal Cartographer has a matrix where each row is sorted left to right and each column is sorted top to bottom. The Cartographer must find a target value in this matrix efficiently, without checking every cell. "Start from the top-right corner," the Cartographer says. "If the current element is greater than the target, move left. If it's less, move down. This eliminates one row or column at each step, running in O(m+n) time." Given an m x n matrix where each row and column is sorted in ascending order, and a target T, find if T exists in the matrix. Output "yes" if found, "no" otherwise. Constraints: 1 <= m, n <= 100, -10^9 <= matrix[i][j], T <= 10^9 Input: 3 3 1 4 7 2 5 8 3 6 9 5 Output: yes Input: 2 2 1 2 3 4 5 Output: no

Constraints:

1 <= m, n <= 100, rows and columns sorted

Tags:

sorted-matrix search matrix
Loading...
Test Cases:No test cases
No test cases available.