LeetCode ยท #74

Search a 2D Matrix Solution

Given an m ร— n matrix with sorted rows and each row's first element greater than the previous row's last, determine if a target exists.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #74

๐Ÿ—๏ธ

Pattern

Binary Search โ€” virtual flattening

Write an efficient algorithm that searches for a value target in an m ร— n integer matrix. Each row is sorted left-to-right, and each row's first integer is greater than the previous row's last integer.

Example
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true
Constraints
โ€ข m == matrix.length, n == matrix[i].length โ€ข 1 โ‰ค m, n โ‰ค 100 โ€ข -10โด โ‰ค matrix[i][j] โ‰ค 10โด
02
Section Two ยท Approach 1

Brute Force โ€” O(m ยท n)

Scan every element. A better approach: binary search the rows to find the right row, then binary search within it โ€” O(log m + log n). But the cleanest optimal solution treats the entire matrix as a single sorted array.

Problem: The matrix is fully sorted when read left-to-right, top-to-bottom. A single binary search on indices 0 to mยทnโˆ’1, mapping index to row/col, gives O(log(mยทn)).
03
Section Three ยท Approach 2

Flattened Binary Search โ€” O(log(mยทn))

Treat the 2D matrix as a virtual 1D sorted array. Index k maps to matrix[k / n][k % n]. Run standard binary search on indices 0 to mยทn โˆ’ 1.

๐Ÿ’ก Mental model: Imagine unrolling the matrix into a single line. Row 0 becomes indices 0..n-1, row 1 becomes n..2n-1, etc. You never physically unroll it โ€” just use arithmetic: row = mid / cols, col = mid % cols. Binary search works on this virtual flat array.
Algorithm
  • Step 1: left = 0, right = m*n - 1.
  • Step 2: Compute mid. Map: row = mid / n, col = mid % n.
  • Step 3: Compare matrix[row][col] to target. Standard binary search logic.
๐ŸŽฏ Alternative โ€” two binary searches:
  • First binary search rows to find which row the target belongs to (compare target to row's first and last element).
  • Then binary search within that row. Same O(log m + log n) = O(log(mยทn)) complexity, but slightly more code.
04
Section Four ยท Trace

Visual Walkthrough

Trace: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3:

Virtual flat array โ€” binary search with index mapping
Virtual: [1,3,5,7,10,11,16,20,23,30,34,60] m=3, n=4 Step 1: L=0,R=11,mid=5 โ†’ row=5/4=1,col=5%4=1 โ†’ matrix[1][1]=11 3 < 11 โ†’ R=4 Step 2: L=0,R=4,mid=2 โ†’ row=0,col=2 โ†’ matrix[0][2]=5 3 < 5 โ†’ R=1 Step 3: L=0,R=1,mid=0 โ†’ row=0,col=0 โ†’ matrix[0][0]=1 3 > 1 โ†’ L=1. mid=1 โ†’ row=0,col=1 โ†’ matrix[0][1]=3 == 3 โœ“ Answer: true โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Flattened Binary Search
class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length, n = matrix[0].length; int left = 0, right = m * n - 1; while (left <= right) { int mid = left + (right - left) / 2; int val = matrix[mid / n][mid % n]; if (val == target) return true; else if (val < target) left = mid + 1; else right = mid - 1; } return false; } }
Python โ€” Flattened Binary Search
class Solution: def searchMatrix(self, matrix: list[list[int]], target: int) -> bool: m, n = len(matrix), len(matrix[0]) left, right = 0, m * n - 1 while left <= right: mid = (left + right) // 2 val = matrix[mid // n][mid % n] if val == target: return True elif val < target: left = mid + 1 else: right = mid - 1 return False
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Brute ForceO(m ยท n)O(1)Scans every element
Staircase (top-right)O(m + n)O(1)Works for LC 240 too but slower
Flattened Binary Search โ† optimal O(log(mยทn)) O(1) Virtual 1D array via index math
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
1ร—1 matrix[[5]], target=5Single element โ†’ mid=0 โ†’ found.
Single row[[1,3,5]]Degenerates to standard 1D binary search.
Single column[[1],[3],[5]]n=1; mid/1=row, mid%1=0. Works correctly.
Not foundtarget=4Falls between 3 and 5 โ†’ L crosses R โ†’ false.
โš  Common Mistake: Using mid / m instead of mid / n for the row. The number of columns (n) determines how many indices fit per row. row = mid / n, col = mid % n. Swapping m and n gives wrong coordinates.

โ† Back to Binary Search problems