Search a 2D Matrix II - Problem

Imagine you have a cleverly organized library where books are arranged in a special way: each row is sorted by publication year (left to right), and each column is sorted by page count (top to bottom). Now you need to find a specific book efficiently!

Given an m x n integer matrix with the following properties:

  • Row-wise sorted: Integers in each row are in ascending order from left to right
  • Column-wise sorted: Integers in each column are in ascending order from top to bottom

Write an efficient algorithm to search for a target value. Return true if found, false otherwise.

Key Challenge: The matrix is partially sorted - it's not a simple 1D sorted array, but has structure we can exploit!

Input & Output

example_1.py โ€” Standard Search
$ Input: matrix = [[1,4,7,11],[2,5,8,12],[3,6,9,16]], target = 5
โ€บ Output: true
๐Ÿ’ก Note: The target 5 exists in the matrix at position [1,1]. Using the optimal approach, we start from top-right (11), move left to 7, then down to 12, left to 8, down to 16, left to 9, down (out of bounds), left to 6, down (out of bounds), left to 3, down (out of bounds), left to 2, down to 5 - found!
example_2.py โ€” Target Not Found
$ Input: matrix = [[1,4,7,11],[2,5,8,12],[3,6,9,16]], target = 13
โ€บ Output: false
๐Ÿ’ก Note: The target 13 does not exist in the matrix. Starting from top-right (11), we move down to 12, down to 16. Since 16 > 13, we move left to 9, but 9 < 13, so we move down (out of bounds). Search space exhausted without finding 13.
example_3.py โ€” Single Element Matrix
$ Input: matrix = [[5]], target = 5
โ€บ Output: true
๐Ÿ’ก Note: Edge case with a single element matrix. The target 5 matches the only element in the matrix at position [0,0].

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 โ‰ค m, n โ‰ค 300
  • -109 โ‰ค matrix[i][j] โ‰ค 109
  • Each row is sorted in non-decreasing order
  • Each column is sorted in non-decreasing order
  • -109 โ‰ค target โ‰ค 109

Visualization

Tap to expand
๐ŸŽฏ Optimal Matrix Search Strategy14711START25TARGET81236916๐Ÿ’ก Key StrategyStart from top-right:โ€ข If current > target โ†’ go LEFTโ€ข If current < target โ†’ go DOWNTime: O(m + n) โšก11 > 5GO LEFT
Understanding the Visualization
1
Start Position
Begin at top-right corner - this position gives you maximum information
2
Smart Decision
If current > target, move left (eliminate column). If current < target, move down (eliminate row)
3
Eliminate Search Space
Each move eliminates an entire row or column, reducing search space efficiently
4
Find or Exhaust
Continue until target is found or search boundaries are exceeded
Key Takeaway
๐ŸŽฏ Key Insight: Starting from the top-right corner allows us to make optimal decisions at each step, eliminating entire rows or columns and achieving linear time complexity!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 25
42.0K Views
High Frequency
~15 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen