# 【LeetCode】84. 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

---------------

2019.06.09

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if (!heights.size()) {
return 0;
}
int result = 0;
for (int i = 0; i < heights.size(); ++ i) {
int min_index = i;
for (int j = i; j < heights.size(); ++ j) {
if (heights[j] < heights[min_index]) {
min_index = j;
}
int sum = heights[min_index] * (j - i + 1);
result = max(result, sum);
}
}
return result;
}
};

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if (!heights.size()) {
return 0;
}

vector<int> start;
start.push_back(0);
for (int i = 1; i < heights.size(); ++ i) {
if (heights[i - 1] < heights[i]) {
start.push_back(i);
}
}

int result = 0;
for (int i = 0; i < start.size(); ++ i) {
int min_index = start[i];
for (int j = start[i]; j < heights.size(); ++ j) {
if (heights[j] < heights[min_index]) {
min_index = j;
}
int sum = heights[min_index] * (j - start[i] + 1);
result = max(result, sum);
}
}
return result;
}
};

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
int result = 0;
stack<int> start;
for (int i = 0; i < heights.size(); ++ i) {
while (!start.empty() && heights[i] <= heights[start.top()]) {
int index = start.top();
start.pop();
result = max(result, heights[index] * (start.empty() ? i : i - start.top() - 1) );
}
start.push(i);
}
return result;
}
};