-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathSorted Matrix Search.cpp
60 lines (35 loc) · 1.48 KB
/
Sorted Matrix Search.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
/*
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
1) Integers in each row are sorted from left to right.
2) The first integer of each row is greater than the last integer of the previous row.
example -
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]], target = 3
Output: true
*/
#include<bits/stdc++.h>
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m=matrix.size();
if(m==0) return false;
int n=matrix[0].size(),low =0,high= m-1,mid;
if(n==0) return false;
if(matrix[0][0] >target) return false;
if(matrix[m-1][n-1]<target ) return false;
while (low<=high){
mid=(low+high)/2;
if(matrix[mid][n-1]==target) return true;
else if (matrix[mid][n-1] < target) low=mid+1;
else high=mid-1;
}
if(matrix[mid][n-1] < target) mid+=1;
if(mid-1>=0 && matrix[mid-1][n-1] >target )
mid-=1;
low=0; high =n-1;
int mi;
while (low<=high){
mi=(low+high)/2;
if(matrix[mid][mi]==target) return true;
else if (matrix[mid][mi] < target) low=mi+1;
else high=mi-1;
}
return false;
}