Entrevista a los 150 mejoresfácil

Buscar una matriz 2D

Guía detallada e implementación de Python para el problema 'Buscar una matriz 2D'.

Declaración del problema

fácil

Se le proporciona una matriz entera m x n matrix con las dos propiedades siguientes:

- Cada fila está ordenada en orden no decreciente.

- El primer número entero de cada fila es mayor que el último número entero de la fila anterior.

Dado un número entero target, devuelve True si target está en matrix o False en caso contrario.

Debe escribir una solución en complejidad temporal O (log (m * n)).

Escribe una función searchMatrix(matrix: List[List[int]], target: int) -> bool.

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

Ejemplos

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 la posibilidad de utilizar estructuras de datos específicas de la búsqueda binaria, como conjuntos o montones.
Edge Cases to Watch
  • Estructuras de entrada vacías
  • Entradas de un solo elemento
  • Grandes límites numéricos

¿Listo para resolver?

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

Abrir en el 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 recomendados de Python

Amplíe sus conocimientos con tutoriales interactivos relacionados, hojas de trucos y comparaciones de códigos.