To determine which row to search, you can check the last element of each row. This can be achieved by using binary search. Then binary search in that row to find the element.
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int m = matrix.length, n = matrix[0].length;
int row = findRow(matrix, target, 0, m - 1);
int left = 0, right = n - 1;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] < target) {
left = mid;
} else {
right = mid;
}
}
return matrix[row][left] == target || matrix[row][right] == target;
}
private int findRow(int[][] matrix, int target, int top, int bottom) {
int last = matrix[0].length - 1;
while (top + 1 < bottom) {
int mid = top + (bottom - top) / 2;
if (matrix[mid][last] >= target) {
bottom = mid;
} else {
top = mid;
}
}
if (target <= matrix[top][last]) {
return top;
}
return bottom;
}
1. BFS, starting from top left node, traverse to right and down direction on each node, until find the target.
T: O(m *n) S: O(m*n) TLE
int[][] dirs = {{0,1}, {1,0}};
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0, 0});
while (!queue.isEmpty()) {
int[] curt = queue.poll();
if (matrix[curt[0]][curt[1]] > target) continue;
if (matrix[curt[0]][curt[1]] == target) {
return true;
}
for (int[] dir : dirs) {
int newx = dir[0] + curt[0];
int newy = dir[1] + curt[1];
if (newx < matrix.length && newy < matrix[0].length) {
queue.offer(new int[]{newx, newy});
}
}
}
return false;
}
2. Divide and Conquer, Divide the matrix into 4 subregion:
Starts from top-right element, move down if matrix[row][col] < target, move left if matrix[row][col] > target.
T: O(m + n)
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int m = matrix.length, n = matrix[0].length;
int row = 0, col = n - 1;
while (row < m && col >= 0) {
if (matrix[row][col] == target) return true;
else if (matrix[row][col] > target) {
col--;
} else {
row++;
}
}
return false;
}
Binary Search between the max and min elements in the matrix. The min is the top-left element, max is the bottom-right element. Each search, take the mid as a threshold, and count how many elements are smaller than the threshold.
T: O(N * log(max - min)) S: O(1)
public int kthSmallest(int[][] matrix, int k) {
int m = matrix.length, n = matrix[0].length;
int left = matrix[0][0], right = matrix[m - 1][n - 1];
while (left < right) {
int mid = left + (right - left) / 2;
if (hasSmaller(matrix, mid) < k) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
private int hasSmaller(int[][] matrix, int x) {
int count = 0;
for (int[] row : matrix) {
int n = matrix[0].length;
if (row[n - 1] <= x) {
count += row.length;
} else {
for (int i = 0; i < n && row[i] <= x; i++) {
count++;
}
}
}
return count;
}