O80

Search a 2D Matrix II

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: BOOLEAN

Problem Statement & Objective

Write an efficient algorithm that searches for an integer target in an m x n matrix where rows and columns are sorted.

Function Signature (Python)

search_matrix(matrix: list[list[int]], target: int) -> bool

Input & Output Contract

Output Rule:True veya False.

System Constraints

m == matris.length, n == matris[i].length
1 <= m, n <= 300
-10^9 <= matris[i][j], hedef <= 10^9

Example Execution Walkthrough

matris = [
 [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]
], hedef = 5 -> True
hedef = 20 -> False

Example Test Scenarios (3)

Example 1: Mevcut
Input:[[[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]],5]
Expected Output:true
Example 2: Yok
Input:[[[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]],20]
Expected Output:false
Example 3: 1x1
Input:[[[-5]],-5]
Expected Output:true
Loading Python Editor...
Input Parameters
[
  [
    [
      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
    ]
  ],
  5
]
Expected Output
true