Leetcode_84_Largest Rectangle in Histogram, leetcode
Please help me increase your popularity. If you have any errors or questions, please leave a message to correct them. Thank you.
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.
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given height = [2, 1, 5, 6, 2, 3],
Return 10
// Vs2012 test code // the core idea of this solution is to calculate the maximum area of the continuously increasing interval at a time, and consider the front and back intervals after completing this interval, will not be affected. That is, the minimum height of the continuous increment interval is greater than or equal to the front and back intervals. # Include <iostream> # include <stack> # include <vector> using namespace std; # define N 6 class Solution {public: int max (int a, int B) {return a> B? A: B;} int largestRectangleArea (vector <int> & height) {height. push_back (0); stack <int> temp; int I = 0; int maxArea = 0; while (I
// Method 1: self-test Accepted // the core idea of this solution is to calculate the maximum area of the continuously increasing interval at a time, and after considering the completion of this interval, when considering its pre-and post-range, it will not be affected. That is, the minimum height of the continuous increment interval is greater than or equal to the front and back intervals. Class Solution {public: int max (int a, int B) {return a> B? A: B;} int largestRectangleArea (vector <int> & height) {height. push_back (0); stack <int> temp; int I = 0; int maxArea = 0; while (I
// Method: timeout int largestRectangleArea (vector <int> & height) {// Start typing your C/C ++ solution below // do not write int main () function int end = height. size (); int begin = 0; int largestarea = 0; for (int I = begin; I <end; ++ I) {int area; int high = height [I]; for (int j = I; j <end; ++ j) {if (height [j]
// Method 2: Other versions // think carefully about the repetitive work. // Take 2 1 3 4 5 as an example. // we already know that R [2] = 4 (height [2] = 3 ), for R [1] (height [1] = 1), since height [1] <= height [2], it is unnecessary to traverse the section 2 to R [2, that is, 3 4 5 ,. // Because height [I] <= height [I + 1] <=... <= height [R [I], // Therefore, if height [I-1] <= height [I], then R [I-1]> = R [I], so we can directly compare height [I-1] and height [R [I] + 1], until the height [I-1]> height [R [j] + 1] is found. Class Solution {public: int largestRectangleArea (vector <int> & height) {// Start typing your C/C ++ solution below // do not write int main () function int n = height. size (); int l [n], r [n]; memset (l, 0, sizeof (int) * n); memset (r, 0, sizeof (int) * n); l [0] = 0; for (int I = 1; I <n; I ++) {if (height [I]> height [I-1]) {l [I] = I;} else {int idx = l [I-1]; while (idx> 0 & height [I] <= height [idx-1]) {idx = l [idx-1];} l [I] = idx ;}} r [n-1] = n-1; for (int I = n-2; I> = 0; I --) {if (height [I]> height [I + 1]) {r [I] = I;} else {int idx = r [I + 1]; while (idx <n-1 & height [I] <= height [idx + 1]) {idx = r [idx + 1];} r [I] = idx ;}} int max = 0; for (int I = 0; I <n; I ++) {if (r [I]-l [I] + 1) * height [I]> max) {max = (r [I]-l [I] + 1) * height [I] ;}} return max ;}};