Le migliori 150 intervisteFacile

Contenitore Con La Maggior Parte Dell'acqua

Guida dettagliata e implementazione Python per il problema "Contenitore con più acqua".

Dichiarazione del problema

Facile

Ti viene fornito un array di numeri interi height di lunghezza n. Sono presenti n linee verticali disegnate in modo tale che i due punti finali della iesima linea siano (i, 0) e (i, height[i]).

Trova due linee che insieme all'asse x formano un contenitore, in modo tale che il contenitore contenga la maggior quantità di acqua.

Restituisce la quantità massima di acqua che un contenitore può immagazzinare.

Tieni presente che non puoi inclinare il contenitore.

Scrivi una funzione maxArea(height: List[int]) -> int.

Vincoli
  • n == len(height)
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4

Esempi

Example 1
Input
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output
49
Explanation

The max area is between lines at index 1 (height 8) and index 8 (height 7). Area = min(8, 7) * (8 - 1) = 7 * 7 = 49.

Example 2
Input
height = [1, 1]
Output
1
Explanation

Area = min(1, 1) * (1 - 0) = 1.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Two Pointers 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.