bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 84. Largest Rectangle in Histogram

84. Largest Rectangle in Histogram

Leetcode Solutions

Using Stack

  1. Initialize a stack to keep indices of bars.
  2. Push -1 onto the stack to serve as a marker.
  3. Iterate over the array of heights. a. While the current bar is shorter than the bar at the top of the stack: i. Pop the top of the stack. ii. Calculate the area with the popped index as the height and the width as the difference between the current index and the new top of the stack minus one. iii. Update the maximum area if necessary. b. Push the current index onto the stack.
  4. After the loop, pop all remaining bars from the stack and calculate the area of rectangles with these bars as the smallest bars.
  5. Return the maximum area found.
UML Thumbnail

Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...