#Leetcode# 84. Largest Rectangle in Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram/
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
Example:
Input: [2,1,5,6,2,3] Output: 10
代码:
class Solution { public: int largestRectangleArea(vector<int>& heights) { int ans = 0; int n = heights.size(); for(int i = 0; i < n; i ++) { if(i + 1 < n && heights[i] <= heights[i + 1]) continue; int minn = heights[i]; for(int j = i; j >= 0; j --) { minn = min(minn, heights[j]); int maxx = minn * (i - j + 1); ans = max(ans, maxx); } } return ans; } };