← Home

74. Search a 2D Matrix

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
74

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 74. Search a 2D Matrix, the solution in this repository is mainly a two pointers solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are searchMatrix.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 8 ms, faster than 92.26% of C++ online submissions for Search a 2D Matrix.
02//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Search a 2D Matrix.
03
04class Solution {
05public:
06    bool searchMatrix(vector<vector<int>>& matrix, int target) {
07        int m = matrix.size();
08        if(m == 0) return false;
09        int n = matrix[0].size();
10        
11        int l = 0, r = m*n-1;
12        
13        while(l <= r){
14            int mid = (l+r)/2;
15            if(matrix[mid/n][mid%n] == target){
16                return true;
17            }else if(matrix[mid/n][mid%n] < target){
18                l = mid+1;
19            }else{
20                r = mid-1;
21            }
22        }
23        
24        return false;
25    }
26};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.