Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
heights = [2,1,5,6,2,3]10heights = [2,4]4heights = [1,1,1,1,1]5maxArea = 0. Iterate i from 0 to n (sentinel h=0 at i=n)i, get h = (i==n) ? 0 : heights[i]h < heights[stack.top]: pop and compute areawidth = stack.empty ? i : i−stack.top−1, update maxi onto stack. Continue until all bars processed.maxAreaWe maintain a monotonically increasing stack of bar indices. When a shorter bar arrives, every taller bar behind it can no longer extend rightward — so we pop each and compute the maximum rectangle it could form. The width is determined by the gap between the current index and the new stack top (left boundary). The sentinel h=0 at position n flushes all remaining bars at the end.