15% off all plans & roadmaps Use code
See plansLeetCode 74 · Solution & editorial
You are given an m x n integer matrix matrix with the following two properties:
Search a 2D Matrix is a Binary Search Patterns problem, in the On Sorted Array/List subpattern. The pattern covers standard binary search, rotated arrays, first/last occurrence, and search on answer space
Master standard binary search on sorted arrays. Learn search insert position, sqrt calculation, and 2D matrix search.
The reference solution runs in O(logm + logn) time and O(1) space.
Search a 2D Matrix has been reported in interviews at Amazon, Facebook, Apple, Microsoft, Nvidia.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Search a 2D Matrix is a Binary Search Patterns problem, in the On Sorted Array/List subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.