Entrevista a los 150 mejoresfácil

Atrapando agua de lluvia

Guía detallada e implementación de Python para el problema 'Atrapamiento de agua de lluvia'.

Declaración del problema

fácil

Dados n enteros no negativos que representan un mapa de elevación donde el ancho de cada barra es 1, calcule cuánta agua puede atrapar después de llover.

Escribe una función trap(height: List[int]) -> int.

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

Ejemplos

Example 1
Input
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output
6
Explanation

The elevation map traps 6 units of rain water. Water fills between the bars: 1 unit above index 2, 1 unit above index 4, 2 units above index 5, 1 unit above index 6, and 1 unit above index 9.

Example 2
Input
height = [4, 2, 0, 3, 2, 5]
Output
9
Explanation

Water trapped: 2 above index 1, 4 above index 2, 1 above index 3, 2 above index 4 = 9 total.

Need a Hint?
Considere la posibilidad de utilizar estructuras de datos específicas de Two Pointers, 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.