18 · Binary Search

#74 Search a 2D Matrix

Mediumsolve on LeetCode ↗

frequently reported atAmazonMicrosoftBloombergGoogleMetaApple

the problem

In plain words

An m×n matrix has each row sorted, and each row's first value is bigger than the previous row's last. Return whether target is in it, in O(log(m·n)).

in matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3

out true

in same matrix, target = 13

out false

  • 1 ≤ m, n ≤ 100
  • -10⁴ ≤ matrix[i][j], target ≤ 10⁴

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #704
      Binary Search
      Easyin Binary Search
    2. #33
      Search in Rotated Sorted Array
      Mediumin Binary Search