240. Search a 2D Matrix II
Last updated
Was this helpful?
Last updated
Was this helpful?
Write an efficient algorithm that searches for a target
value in an m x n
integer matrix
. The matrix
has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example 1:
Example 2:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-10^9 <= matix[i][j] <= 10^9
All the integers in each row are sorted in ascending order.
All the integers in each column are sorted in ascending order.
-10^9 <= target <= 10^9
Array, Binary Search, Divide and Conquer
Perform a search which starts from the bottom-left element, and return true only if target
is found. If the element is larger than target
, check the element just above it. Otherwise, check the element to its right. Return false if the search finishes.
Time complexity:
Space complexity: