Entrevista a los 150 mejoresfácil

Rectángulo más grande en histograma

Guía detallada e implementación Python para el problema 'Rectángulo más grande en histograma'.

Declaración del problema

fácil

Dada una matriz de números enteros heights que representan la altura de la barra del histograma donde el ancho de cada barra es 1, devuelve el área del rectángulo más grande del histograma.

Escribe una función largestRectangleArea(heights: List[int]) -> int.

Restricciones
  • 1 <= len(heights) <= 10^5
  • 0 <= heights[i] <= 10^4

Ejemplos

Example 1
Input
heights = [2, 1, 5, 6, 2, 3]
Output
10
Explanation

The largest rectangle has area 10, formed by bars at index 2 and 3 (heights 5 and 6), with width 2 and height 5.

Example 2
Input
heights = [2, 4]
Output
4
Explanation

The largest rectangle is the bar at index 1 with height 4 and width 1, giving area 4.

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