JS O(m + n)


  • 0
    Y
    function searchMatrix(matrix, query) {
      let minRow = 0
      let maxCol = matrix[0].length - 1
      while (minRow < matrix.length && 0 <= maxCol) {
        const element = matrix[minRow][maxCol]
        if (element === query) {
          return true
        } else if (element < query) {
          minRow += 1
        } else {
          maxCol -= 1
        }
      }
      return false
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.