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.

Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]], target = 13
Output: false

Example 3:

Input: matrix = [], target = 0
Output: false

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 0 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104

Solution

Time complexity : O(log(mn))
Space complexity : O(1)

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        if (0 == m) return false;
        int n = matrix[0].size();
        if (0 == n) return false;
        
        int len = m * n;
        int l = 0, r = len - 1;
        while (l < r) {
            int mid = (l+r)>>1;
            int val = matrix[mid/n][mid%n];
            if (val == target)
                return true;
            else if (target < val)
                r = mid;
            else l = mid + 1;
        }
        return matrix[l/n][l%n] == target;
    }
};

因上一列最後一個元素<此列第一個元素,因此整個2D matrix可視為1D sorted array。

用Binary Search,求出結果。

要注意其中座標位置的轉換。