Search A 2D Matrix
To solve this coding challenge, we need to efficiently search for a target value in a 2D matrix that has distinct, sorted rows and columns. Since rows are sorted in non-decreasing order and the first integer of each row is greater than the last integer of the previous row, the matrix can be visualized as a single flattened, sorted array. This allows us to use binary search to achieve an O(log(m*n)) time complexity.
# Explanation:
- Matrix and Binary Search Setup :
- Each element in the matrix can be uniquely indexed using two coordinates, namely row and column indices.
- Since the matrix elements are sorted in a row-major order, it's feasible to map a 2D index to a 1D index, which is conducive to applying binary search.
- Defining Matrix Dimensions :
-
We determine the number of rows
and columns
mfrom the matrix.n - Binary Search Implementation :
-
Initialize two pointers
and
leftto the beginning and end of the hypothetical 1D array that the 2D matrix represents.right -
Calculate the middle index
and map it back to its 2D coordinates (row and column).
mid -
Compare the middle element with the target. If it matches, return
.
True - Adjust the search range based on whether the middle element is less than or greater than the target.
-
If pointers cross without finding the element, return
.
False - Matrix Initialization :
-
calculates the number of rows in the matrix.
rows = len(matrix) -
calculates the number of columns in the matrix.
columns = len(matrix[0]) - Binary Search Initialization :
-
sets the left boundary to the start of the 1D representation of the matrix.
left = 0 -
sets the right boundary to the end of the 1D representation.
right = (rows * columns) - 1 - Binary Search Loop :
-
Mid Calculation
:
calculates the middle index.
mid = left + (right - left) // 2 - Mapping to 2D Coordinates :
-
maps the middle index to the corresponding row.
row_index = mid // columns -
maps the middle index to the corresponding column.
column_index = mid % columns -
retrieves the middle element from the matrix.
mid_element = matrix[row_index][column_index] - Comparison :
-
If
: Return
mid_element == target(target found).True -
If
: Move the
mid_element < targetpointer toleftto search the right half.mid + 1 -
If
: Move the
mid_element > targetpointer torightto search the left half.mid - 1 - End Condition :
-
If the loop exits without finding the target, return
.
False
# Pseudocode with Comments:
# Given a matrix and a target value
# Initialize matrix dimensions
rows = number of rows in matrix
columns = number of columns in matrix
# Initialize search boundary pointers
left = 0
right = (rows * columns) - 1
# Conduct binary search
while left <= right:
# Calculate the middle index
mid = left + (right - left) // 2
# Map mid index back to 2D coordinates
row_index = mid // columns
column_index = mid % columns
mid_element = matrix[row_index][column_index]
# Compare the middle element with the target value
if mid_element == target:
return True # Target found
elif mid_element < target:
left = mid + 1 # Adjust the search to the right half
else:
right = mid - 1 # Adjust the search to the left half
# Target is not found in the matrix
return False