[InterviewBit] Matrix Search

Matrix Search

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
int Solution::searchMatrix(vector<vector<int> > &A, int B) {
int l = 0, r = A.size() - 1;
while(l <= r) {
int m = l + (r - l) / 2;
if(A[m].front() <= B and B <= A[m].back()) return *lower_bound(begin(A[m]), end(A[m]), B) == B;
else if(A[m].back() < B) l = m + 1;
else r = m - 1;
}
return 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/30/PS/interviewbit/matrix-search/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.