A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 240. Search a 2D Matrix II, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. 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: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are searchMatrix, binarySearch.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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(m+n)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 284 ms, faster than 13.67% of C++ online submissions for Search a 2D Matrix II.
02//Memory Usage: 12.3 MB, less than 100.00% of C++ online submissions for Search a 2D Matrix II.
03class Solution {
04public:
05 bool searchMatrix(vector<vector<int>>& matrix, int target) {
06 int m = matrix.size();
07 if(m == 0) return false;
08 int n = matrix[0].size();
09 if(n == 0) return false;
10 int r = n;
11 // cout << m << " " << n << " " << r << endl;
12
13 for(vector<int>& row : matrix){
14 //upper_bound: Unlike lower_bound, the value pointed by the iterator returned by this function cannot be equivalent to val, only greater.
15 //the search range becomes smaller and smaller
16 auto it = upper_bound(row.begin(), row.begin() + r, target);
17 //if we've found an element larger than target, we can shrink the range
18 //o.w. the range remains the same
19 if(it != row.begin() + r){
20 r = it - row.begin();
21 }
22 //r is the position where row[r] > target(notice it's not >= !!)
23 // cout << r << endl;
24 /*
25 row[r] is the minimum element we can find,
26 and it is larger than target,
27 so we will not find target in the remaining part of the matrix
28 */
29 if(r == 0) return false;
30 if(row[r-1] == target) return true;
31 }
32
33 return false;
34 }
35};
36
37//https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/66140/My-concise-O(m%2Bn)-Java-solution
38//Runtime: 64 ms, faster than 85.79% of C++ online submissions for Search a 2D Matrix II.
39//Memory Usage: 12.1 MB, less than 100.00% of C++ online submissions for Search a 2D Matrix II.
40//time: O(m+n)
41class Solution {
42public:
43 bool searchMatrix(vector<vector<int>>& matrix, int target) {
44 int m = matrix.size();
45 if(m == 0) return false;
46 int n = matrix[0].size();
47 if(n == 0) return false;
48
49 int row = 0, col = n-1;
50 while(col >= 0 && row < m){
51 if(matrix[row][col] == target){
52 return true;
53 }else if(target < matrix[row][col]){
54 //the current column's value are all larger than target
55 col--;
56 }else{
57 //the current row's value are all smaller than target
58 row++;
59 }
60 }
61
62 return false;
63 }
64};
65
66//divide and conquer
67//https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/66285/C%2B%2BC-divide-and-conquer-solution
68//Runtime: 160 ms, faster than 22.49% of C++ online submissions for Search a 2D Matrix II.
69//Memory Usage: 12.5 MB, less than 100.00% of C++ online submissions for Search a 2D Matrix II.
70class Solution {
71public:
72 bool binarySearch(vector<vector<int>>& matrix, int target
73 , int row_min, int row_max, int col_min, int col_max){
74 if(row_min > row_max) return false;
75 if(col_min > col_max) return false;
76 int row = (row_min + row_max)/2;
77 int col = (col_min + col_max)/2;
78
79 if(target == matrix[row][col]){
80 return true;
81 }else if(target > matrix[row][col]){
82 //exclude matrix[row_min:row][col_min:col] from matrix[row_min:row_max][col_min:col_max]
83 return binarySearch(matrix, target, row_min, row_max, col+1, col_max) ||
84 binarySearch(matrix, target, row+1, row_max, col_min, col);
85 }else{
86 //these two rectangle together with matrix[row][col] construct matrix[row_min:row][col_min:col]
87 return binarySearch(matrix, target, row_min, row_max, col_min, col-1) ||
88 binarySearch(matrix, target, row_min, row-1, col, col_max);
89 }
90 };
91
92 bool searchMatrix(vector<vector<int>>& matrix, int target) {
93 int m = matrix.size();
94 if(m == 0) return false;
95 int n = matrix[0].size();
96 if(n == 0) return false;
97 return binarySearch(matrix, target, 0, m-1, 0, n-1);
98 }
99};
Cost