Search a 2D Matrix

Desicription

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

1
2
3
4
5
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty() || matrix[0].empty())
return 0;
vector<int> col(matrix.size());
for(int i = 0; i < matrix.size(); ++i)
col[i] = matrix[i][0];
int index = lower_bound(col.begin(), col.end(), target) - col.begin();
if(!index && col[index] > target)
return 0;
if(index == matrix.size() ||(index && matrix[index][0] > target))
--index;
return *lower_bound(matrix[index].begin(), matrix[index].end(), target) == target;
}
};