Search for a command to run...
Start at top-right corner to eliminate rows or columns progressively
Current step
Search for target 5 in 5x5 matrix. Each row and column is sorted in ascending order. Start from the top-right corner.
Step-Wise Search
1class Solution {2 searchMatrix(matrix, target) {3 let n = matrix.length;4 let m = matrix[0].length;5 6 let row = 0, col = m - 1;7 8 while (row < n && col >= 0) {9 if (matrix[row][col] === target) return true;10 else if (matrix[row][col] < target) row++;11 else col--;12 }13 return false;14 }15}