Skip to contentKEEP THE CRAFTTHE SANCTUARY

Binary Search a Globally Sorted Matrix

Search a matrix whose rows, read one after another, form a single sorted list.

The problem

Implement Solution.searchMatrix(matrix, target). matrix has between 1 and 100 rows and between 1 and 100 columns. Each row is sorted in increasing order, and the first number of each row is larger than the last number of the row above it. Return True if target is in matrix and False otherwise. Your solution must run in O(log(rows * cols)) time.

Starter code

class Solution:
    def searchMatrix(self, matrix, target):
        pass

Public examples

  1. matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], target = 16givesTrue
  2. matrix = [[1, 3, 5], [8, 10, 12]], target = 12givesTrue
  3. matrix = [[1, 3, 5], [8, 10, 12]], target = 9givesFalse

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.