Largest Rectangle in a Histogram

Given a nonempty array nums of histogram bar heights, where each bar has width one, return the largest rectangular area that fits under consecutive bars.…

Given a nonempty array `nums` of histogram bar heights, where each bar has width one, return the largest rectangular area that fits under consecutive bars. Every bar height is nonnegative. The rectangle cannot extend above any covered bar. Return only the area, and do not change the input array.

- `1 <= nums.length <= 100000` - `0 <= nums[i] <= 10000` - Each bar has width one; a zero-height histogram has largest area zero.

Example 1: input nums = [2,1,5,6,2,3]; output 10. The consecutive heights 5 and 6 support a height-5, width-2 rectangle.

Example 2: input nums = [3,5,1,7,5,9]; output 15. The final three bars support a height-5, width-3 rectangle of area 15.

Example 3: input nums = [2,4]; output 4. Either both bars at height 2, or the second bar alone at height 4, gives area 4.

Category: Stack. Topic: stack. Difficulty: Hard. Patterns: monotonic-stack.