Le migliori 150 intervisteFacile

Cerca una matrice 2D

Guida dettagliata e implementazione Python per il problema "Cerca una matrice 2D".

Dichiarazione del problema

Facile

Ti viene data una matrice intera m x n matrix con le seguenti due proprietà:

- Ogni riga è ordinata in ordine non decrescente.

- Il primo intero di ogni riga è maggiore dell'ultimo intero della riga precedente.

Dato un numero intero target, restituisce True se target è in matrix o False altrimenti.

È necessario scrivere una soluzione in complessità temporale O(log(m * n)).

Scrivi una funzione searchMatrix(matrix: List[List[int]], target: int) -> bool.

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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche della ricerca binaria come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.