nathanwailes

LeetCode 84 - Largest Rectangle in Histogram - 2023.11.09 solution

Nov 8th, 2023
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class Solution:
  2.     def largestRectangleArea(self, heights: List[int]) -> int:
  3.         area_of_largest_rectangle = 0
  4.         heights_to_start_indices = []
  5.         i = 0
  6.  
  7.         for i, h in enumerate(heights):
  8.             area_of_largest_rectangle = max(area_of_largest_rectangle, h)
  9.            
  10.             while len(heights_to_start_indices) > h:
  11.                 heights_to_start_indices.pop()
  12.            
  13.             for hj, j in enumerate(heights_to_start_indices):
  14.                 area = (i - j + 1) * (hj + 1)
  15.                 area_of_largest_rectangle = max(area_of_largest_rectangle, area)
  16.            
  17.             while h > len(heights_to_start_indices):
  18.                 heights_to_start_indices.append(i)
  19.            
  20.             i += 1
  21.        
  22.         return area_of_largest_rectangle
Advertisement
Add Comment
Please, Sign In to add comment