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.
Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units.
Input: heights = [2,4] Output: 4
1 <= heights.length <= 105
0 <= heights[i] <= 104
impl Solution {
pub fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
let mut stack = vec![];
let mut widths = vec![1; heights.len()];
let mut ret = 0;
for i in 0..heights.len() {
while stack.last().unwrap_or(&(0, -1)).1 >= heights[i] {
stack.pop();
}
widths[i] = i as i32 - stack.last().unwrap_or(&(-1, 0)).0;
stack.push((i as i32, heights[i]));
}
stack.clear();
for i in (0..heights.len()).rev() {
while stack.last().unwrap_or(&(0, -1)).1 >= heights[i] {
stack.pop();
}
widths[i] += stack.last().unwrap_or(&(heights.len() as i32, 0)).0 - 1 - i as i32;
stack.push((i as i32, heights[i]));
ret = ret.max(widths[i] * heights[i]);
}
ret
}
}