Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Search in A Sorted 2D Matrix II

Medium Acceptance 70.00% Points 30.00

You are given an m × n integer matrix matrix where each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Given an integer target, return true if the target exists in the matrix, otherwise return false.

Examples
Example 1

Example 1

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5

Output: true

Explanation: The element 5 exists in the matrix at row 2, column 2, so the result is true.

Example 2

Example 2

Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20

Output: false

Explanation: The element 20 does not exist in any row or column of the matrix, so the result is false.

Example 3

Example 3

Input: matrix = [[2,6,10],[5,9,13],[8,12,15]], target = 12

Output: true

Explanation: The element 12 exists in the matrix at row 3, column 2, so the result is true.

Hints
Hint 1
N/A
Constraints
  • 1 = m, n = 300
  • -10? = matrix[i][j], target = 10?
Companies
Microsoft Apple
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions