150 principais entrevistasFácil

Pesquise uma matriz 2D

Guia detalhado e implementação de Python para o problema 'Pesquisar uma matriz 2D'.

Declaração do problema

Fácil

Você recebe uma matriz inteira m x n matrix com as duas propriedades a seguir:

- Cada linha é classificada em ordem não decrescente.

- O primeiro inteiro de cada linha é maior que o último inteiro da linha anterior.

Dado um número inteiro target, retorne True se target estiver em matrix ou False caso contrário.

Você deve escrever uma solução com complexidade de tempo O(log(m * n)).

Escreva uma função searchMatrix(matrix: List[List[int]], target: int) -> bool.

Restrições
  • m == len(matrix)
  • n == len(matrix[i])
  • 1 <= m, n <= 100
  • -10^4 <= matrix[i][j], target <= 10^4

Exemplos

Example 1
Input
matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], target = 3
Output
True
Explanation

3 is found in the first row at index 1.

Example 2
Input
matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], target = 13
Output
False
Explanation

13 is not present in the matrix.

Need a Hint?
Considere usar estruturas de dados específicas da pesquisa binária, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.