[AlgoExpert] Search In Sorted Matrix

Search In Sorted Matrix

  • Time : O(n + m)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
vector<int> searchInSortedMatrix(vector<vector<int>> matrix, int target) {
int n = matrix.size(), m = matrix[0].size();
int i = 0, j = m - 1;
while(j >= 0 and i < n) {
if(matrix[i][j] == target) return {i, j};
if(matrix[i][j] > target) j--;
else i++;
}
return {-1, -1};
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/14/PS/AlgoExpert/search-in-sorted-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.