## # 240. Search a 2D Matrix II

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 in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. Example:

Consider the following matrix:

``````[
[1,   4,  7, 11, 15],
[2,   5,  8, 12, 19],
[3,   6,  9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
``````

Given target = 5, return true.

Given target = 20, return false.

### # Solution

Approach 1: O(mlogn).

Approach 2: O(m+n).

Approach 3: O(logm+logn).

Approach 4: O(log4_(m*n))?

Approach 1:

Approach 2:

### # Code (C++)

Approach 1:

``````// O(mlogn)
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
for (int i = 0; i < m; ++i)
{
int lo = 0;
int hi = matrix[i].size() - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (target < matrix[i][mid])
hi = mid - 1;
else if (target > matrix[i][mid])
lo = mid + 1;
else
return true;
}
}
return false;
}
};
``````

Approach 2:

``````// O(m+n)
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (m == 0) return false;
int n = matrix[0].size();
if (n == 0) return false;
int i = 0;
int j = n - 1;
while (i < m && j >= 0)
{
if (target < matrix[i][j])
j--;
else if (target > matrix[i][j])
i++;
else
return true;
}
return false;
}
};
``````

Approach 3:

``````// O(logm+logn)
class Solution {
private:
int binarySearchCol(vector<vector<int>>& matrix, int i, int j, int target) {
int lo = 0;
int hi = j;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (target < matrix[i][mid])
hi = mid - 1;
else if (target > matrix[i][mid])
lo = mid + 1;
else
return mid;
}
return lo - 1;
}
int binarySearchRow(vector<vector<int>>& matrix, int i, int j, int target) {
int lo = 0;
int hi = i;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (target < matrix[mid][j])
hi = mid - 1;
else if (target > matrix[mid][j])
lo = mid + 1;
else
return mid;
}
return lo;
}
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (m == 0) return false;
int n = matrix[0].size();
if (n == 0) return false;
int i = 0;
int j = n - 1;
while (i < m && j >= 0)
{
if (target < matrix[i][j])
j = binarySearchCol(matrix, i, j, target);
else if (target > matrix[i][j])
i = binarySearchRow(matrix, i, j, target);
else
return true;
}
return false;
}
};
``````

Approach 4:

``````// O(log4_(m*n)?)
class Solution {
private:
bool doSearchMatrix(vector<vector<int>>& matrix, int i1, int j1, int i2, int j2, int target) {
if (i1 < 0 || i1 >= matrix.size() ||
i2 < 0 || i2 >= matrix.size() ||
j1 < 0 || j1 >= matrix[0].size() ||
j2 < 0 || j2 >= matrix[0].size() ||
target < matrix[i1][j1] || target > matrix[i2][j2])
{
return false;
}
if (target == matrix[i1][j1] || target == matrix[i2][j2])
return true;
int midI = (i1 + i2) / 2;
int midJ = (j1 + j2) / 2;
return
doSearchMatrix(matrix, i1, j1, midI, midJ, target) ||
doSearchMatrix(matrix, midI+1, j1, i2, midJ, target) ||
doSearchMatrix(matrix, i1, midJ+1, midI, j2, target) ||
doSearchMatrix(matrix, midI+1, midJ+1, i2, j2, target);
}
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (m == 0) return false;
int n = matrix[0].size();
if (n == 0) return false;
return doSearchMatrix(matrix, 0, 0, m-1, n-1, target);
}
};
``````