Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Search in A Sorted 2D Matrix II

Medium Acceptance 70% Points 30

You are given an m × n integer matrix matrix where each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Given an integer target, return true if the target exists in the matrix, otherwise return false.

Examples
Example 1

Example 1

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5

Output: true

Explanation: The element 5 exists in the matrix at row 2, column 2, so the result is true.

Example 2

Example 2

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20

Output: false

Explanation: The element 20 does not exist in any row or column of the matrix, so the result is false.

Example 3

Example 3

Input: matrix = [[2,6,10],[5,9,13],[8,12,15]], target = 12

Output: true

Explanation: The element 12 exists in the matrix at row 3, column 2, so the result is true.

Constraints
  • 1 = m, n = 300
  • -10? = matrix[i][j], target = 10?
Topics
Binary Search
Companies
Microsoft Apple
Added 19 Jul 2026 70% acceptance
Ln 1, Col 1Sign in to save your code
1 4 7 11 15
2 5 8 12 19
3 6 9 16 22
10 13 14 17 24
18 21 23 26 30
5
true