Search a 2D Matrix I & II

Part I

[Leetcode 74]
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,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
Given target = 3, return true.

View the matrix as a single array.

// we can view this matrix as an sorted array matrix[0, m *n - 1], given a index in array
// we can use (index / n) to get row index and use (index % n) to get column index
// then use binary search to find the target
public boolean searchMatrix(int[][] matrix, int target) {
        // check corner case
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
            return false;
        }
        int m = matrix.length, n = matrix[0].length;

        int start = 0, end = m * n - 1;
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (matrix[mid / n][mid % n] == target) {
                return true;
            } else if (matrix[mid / n][mid % n] > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }

        return false;
    }


Part 2

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.
For 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.
// Start searching from Upper Right Corner
// if equal, we found it!
// if less than target, go to the row below
// if larger than target, go to the column on left
public boolean searchMatrix(int[][] matrix, int target) {
        // check corner case
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return false;
        int m = matrix.length, n = matrix[0].length;
        int row = 0, col = n - 1;

        while (row < m && col >= 0) {
            if (matrix[row][col] == target) { // found it
                return true;   
            } else if (matrix[row][col] < target) { // go next row
                row++;
            } else { // go left column
                col--;
            }
        }

        return false;
    }


Part 2 follow up

What if the target can appear multiple times in the matrix?

Solution:
If we found target at matrix[row][col], then we know:
matrix[row][col - 1] must be less than target.
matrix[row + 1][col] must be larger than target;
Thus, we go directly to matrix[row + 1][col - 1].
Which is equal to count++, row++, col--;

results matching ""

    No results matching ""