We need to efficiently search for a target value in a matrix where rows and columns are sorted in ascending order. The matrix is structured such that lower values are towards the top-left corner and higher values are towards the bottom-right corner.
Approach
The provided solution utilizes a two-pointer approach starting from the bottom-left corner of the matrix:
Begin from the bottom-left corner (matrix[n-1][0]).
If the current value is equal to the target, return true.
If the current value is greater than the target, move upwards to the next row (row--).
If the current value is less than the target, move to the right to the next column (col++).
This approach efficiently narrows down the search space by leveraging the sorted properties of both rows and columns.
Description
We need to efficiently search for a target value in a matrix where rows and columns are sorted in ascending order. The matrix is structured such that lower values are towards the top-left corner and higher values are towards the bottom-right corner.
Approach
The provided solution utilizes a two-pointer approach starting from the bottom-left corner of the matrix:
Begin from the bottom-left corner (matrix[n-1][0]).
If the current value is equal to the target, return true.
If the current value is greater than the target, move upwards to the next row (row--).
If the current value is less than the target, move to the right to the next column (col++).
This approach efficiently narrows down the search space by leveraging the sorted properties of both rows and columns.
Put check marks:
How Has This Been Tested?
[x] Implemented test cases for matrix search.
Test Case 1:
Input Matrix:
Target: 5
Expected Output: True
Actual Output: True (verified)
Test Case 2:
Input Matrix:
Target: 20
Expected Output: False
Actual Output: False (verified)
Make sure all below guidelines are followed else PR will get Reject: