-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0074-search-a-2d-matrix.c
47 lines (35 loc) · 1.21 KB
/
0074-search-a-2d-matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given a 2d matrix, where,
1. the rows are sorted from left to right,
2. the last element of each row is less than first element of next row
Find an efficient method to search for a given element in this array
Ex. matrix = [[ 1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 60]],
target = 3 -> true (3 exists)
The 2D matrix can be thought of as a 1D array if the rows are arranged
sequentially. And given the conditions, the 1D array will also be sorted.
So, binary search can be used to solve this problem with indexes (row, col)
rather than one.
Time: O(log(mn))
Space: O(1)
*/
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target){
int m = matrixSize;
int n = *matrixColSize;
int low = 0;
int high = m*n - 1;
int mid = low + (high - low)/2;
while (low <= high) {
int row = mid / n;
int col = mid % n;
if (matrix[row][col] > target)
high = mid - 1;
else if (matrix[row][col] < target)
low = mid + 1;
else
return true;
mid = low + (high - low)/2;
}
return false;
}