Loading content...
You are given an array of non-negative integers representing the heights of vertical bars arranged side by side, where each bar has a uniform width of exactly one unit. These bars together form a skyline-like structure when viewed from the front.
Your task is to determine the maximum area of a rectangle that can be inscribed within this bar structure. The rectangle must be formed using contiguous bars, and its height is limited by the shortest bar within the selected contiguous segment. The rectangle's width equals the number of contiguous bars included.
In other words, for any contiguous segment of bars, the largest rectangle you can form has:
Find and return the maximum possible rectangular area achievable across all possible contiguous segments of bars.
heights = [2,1,5,6,2,3]10Consider the contiguous segment from index 2 to index 3 (heights 5 and 6). The minimum height in this range is 5, and the width is 2 bars. Area = 5 × 2 = 10. While other combinations exist (e.g., the entire array with height 1 gives area 6), this segment produces the maximum rectangular area of 10 square units.
heights = [2,4]4We can either select just the second bar (height 4, width 1, area = 4) or both bars together (minimum height 2, width 2, area = 4). Both options yield the maximum area of 4 square units.
heights = [1,2,3,4,5]9The optimal rectangle is formed by the contiguous segment from index 2 to index 4 (heights 3, 4, 5). The minimum height in this range is 3, and the width is 3 bars. Area = 3 × 3 = 9 square units. Using all 5 bars would give height 1 × width 5 = 5, which is smaller.
Constraints