-
Notifications
You must be signed in to change notification settings - Fork 0
/
0074. Search a 2D Matrix.cpp
57 lines (51 loc) · 1.46 KB
/
0074. Search a 2D Matrix.cpp
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
48
49
50
51
52
53
54
55
56
57
//brute force solution
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
// running a forloop till size of matrix
for(int i = 0; i<matrix.size(); ++i)
{
// nested for for ietrating each row element
for(int j = 0; j<matrix[0].size(); ++j)
{
// if found return true
if(matrix[i][j] == target)
return true;
}
}
// after traversal if not found
// return false
return false;
}
// for github repository link go to my profile.
};
// optimized solution with binary search
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int n = matrix.size();
int m = matrix[0].size();
// if matrix have 0 rows or 0 colums
if(n == 0 || m == 0)
return false;
// treating matrix as array just taking care of endices
// [0..n*m]
int start = 0, end = m*n - 1;
while(start <= end)
{
int mid = start + (end - start) / 2;
// a[x] : matrix[x / m][x % m] formulae
int ind = matrix[mid/m][mid%m];
if (target == ind)
return true;
// left half
else if(target < ind)
end = mid - 1;
else
// right half
start = mid + 1;
}
return false;
}
// for github repository link go to my profile.
};