Problem Description
Write an efficient algorithm that searches for a value target
in an m x n
integer matrix 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.
Examples
Example 1: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true Example 2: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 Output: false
Python Solution
def searchMatrix(matrix: List[List[int]], target: int) -> bool:
if not matrix or not matrix[0]:
return False
m, n = len(matrix), len(matrix[0])
left, right = 0, m * n - 1
while left <= right:
mid = (left + right) // 2
row, col = mid // n, mid % n
value = matrix[row][col]
if value == target:
return True
elif value < target:
left = mid + 1
else:
right = mid - 1
return False
Java Solution
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int right = m * n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int row = mid / n;
int col = mid % n;
int value = matrix[row][col];
if (value == target) {
return true;
} else if (value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}
C++ Solution
class Solution {
public:
bool searchMatrix(vector>& matrix, int target) {
if (matrix.empty() || matrix[0].empty()) {
return false;
}
int m = matrix.size();
int n = matrix[0].size();
int left = 0;
int right = m * n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int row = mid / n;
int col = mid % n;
int value = matrix[row][col];
if (value == target) {
return true;
} else if (value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
};
JavaScript Solution
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
if (!matrix.length || !matrix[0].length) {
return false;
}
const m = matrix.length;
const n = matrix[0].length;
let left = 0;
let right = m * n - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const row = Math.floor(mid / n);
const col = mid % n;
const value = matrix[row][col];
if (value === target) {
return true;
} else if (value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
};
C# Solution
public class Solution {
public bool SearchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.Length == 0 || matrix[0].Length == 0) {
return false;
}
int m = matrix.Length;
int n = matrix[0].Length;
int left = 0;
int right = m * n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int row = mid / n;
int col = mid % n;
int value = matrix[row][col];
if (value == target) {
return true;
} else if (value < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}
Complexity Analysis
- Time Complexity: O(log(m×n)) using binary search
- Space Complexity: O(1) using only constant extra space
Solution Explanation
This solution treats the 2D matrix as a sorted 1D array:
- Key insight:
- Matrix properties allow treating it as 1D sorted array
- Use binary search on flattened array
- Convert mid index to row and column
- Algorithm steps:
- Handle edge cases
- Binary search on range [0, m×n-1]
- Convert index to 2D coordinates:
- row = mid / n
- col = mid % n
- Compare and update search range
Key points:
- Efficient binary search
- No extra space needed
- Handles all edge cases
- Simple index conversion