Prompt
You are given an m x n integer matrix matrix with the following two properties:
- Each row is sorted in non-decreasing order.
- The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.

Solution
In C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
const int rows = matrix.size();
const int cols = matrix[0].size();
int l = 0; int r = rows * cols - 1;
while (l <= r) {
int m = (r - l) / 2 + l;
int num = matrix[m / cols][m % cols];
if (target < num) r = m - 1;
else if (num < target) l = m + 1;
else return true;
}
return false;
}Explanation
Do 704. Binary Search by indexing the matrix as a 1d array.
Big O Analysis
Time Complexity