impl Solution {
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
// Get the number of rows (m) and columns (n) in the matrix
let m = matrix.len();
let n = matrix[0].len();
// Initialize two pointers: left and right
let mut left = 0;
let mut right = (m * n) as i32 - 1;
// Perform binary search on the flattened matrix
while left <= right {
// Calculate the middle index
let mid = left + (right - left) / 2;
// Convert the middle index to row and column indices
let row = (mid as usize) / n;
let col = (mid as usize) % n;
// Get the middle element
let mid_element = matrix[row][col];
// If the middle element is equal to the target, return true
if mid_element == target {
return true;
}
// If the middle element is greater than the target,
// search the left half of the matrix
if mid_element > target {
right = mid - 1;
}
// If the middle element is less than the target,
// search the right half of the matrix
else {
left = mid + 1;
}
}
// If the target is not found, return false
false
}
}