LeetCode //C - 74. Search a 2D Matrix

74. Search a 2D Matrix

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.
 

Example 1:

LeetCode //C - 74. Search a 2D Matrix_第1张图片

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

Example 2:

LeetCode //C - 74. Search a 2D Matrix_第2张图片

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

Constraints:
  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • − 1 0 4 < = m a t r i x [ i ] [ j ] , t a r g e t < = 1 0 4 -10^4 <= matrix[i][j], target <= 10^4 104<=matrix[i][j],target<=104

From: LeetCode
Link: 74. Search a 2D Matrix


Solution:

Ideas:

1. Mapping 2D to 1D: Even though the matrix is 2-dimensional, we can view it as a 1-dimensional array (by stacking rows one after the other). For instance, in a matrix with n columns, the element in the 2nd row and 1st column can be accessed by the index n of our “flattened” array.

2. Binary Search: We define two pointers, left and right, to represent the start and end of our current search space. Initially, left is at the beginning and right is at the end of our “flattened” matrix.

3. Midpoint Calculation: For each iteration, we find the midpoint of our current search space. We then convert this midpoint index into its corresponding 2D matrix coordinates using the formulas:

  • mid / number_of_columns gives the row.
  • mid % number_of_columns gives the column.

4. Comparison: Once we have the 2D coordinates for our midpoint, we can retrieve the value from the matrix and compare it to our target:

  • If the value is equal to the target, we’ve found the element and return true.
  • If the value is less than the target, it means our desired value (if it exists) is to the right of our current midpoint in our “flattened” view. So, we adjust our left pointer to mid + 1.
  • If the value is greater than the target, our desired value is to the left of our current midpoint. We then adjust our right pointer to mid - 1.

5. Convergence: We continue the binary search process, narrowing down our search space, until our left pointer exceeds the right pointer or we find the target.

6. Result: If we’ve gone through the entire search space without finding the target, we return false. Otherwise, we return true when we find the target.

Code:
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {
    int m = matrixSize;
    int n = *matrixColSize;
    int left = 0, right = m * n - 1;
    
    while (left <= right) {
        int mid = left + (right - left) / 2;
        int mid_value = matrix[mid / n][mid % n];
        
        if (mid_value == target) {
            return true;
        } else if (mid_value < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return false;
}

你可能感兴趣的:(LeetCode,leetcode,c语言,算法)