-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path74_search_2D_matrix.cpp
73 lines (60 loc) · 1.8 KB
/
74_search_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
*/
// TC = O(log(m * n))
#include <bits/stdc++.h>
using namespace std;
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
int col = matrix[0].size();
int left = 0;
int right = row * col -1;
while(left <= right){
int mid = (left+right)/2;
int midVal = matrix[mid /col][mid % col];
if(midVal == target) {
return true;
}
else if(midVal > target) {
right = mid - 1;
}
else{
left = mid + 1;
}
}
return false;
}
/*
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size();
int col = matrix[0].size();
for(int i = 0; i< row ; i++) {
int start = 0;
int end = col - 1;
while(start <= end){
int mid = (start + end)/2;
if(matrix[i][mid] == target) {
return true;
}
else if(matrix[i][mid] > target) {
end = mid - 1;
}
else{
start = mid + 1;
}
}
}
return false;
*/
int main() {
vector<vector<int>> matrix = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
int target = 3;
cout << (searchMatrix(matrix, target) ? "True" : "False") << endl;
return 0;
}