Leetcode: largest rectangle in Histogram

Source: Internet
Author: User

Leetcode: largest rectangle in Histogram

GivenNNon-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 =10Unit.

For example,
Given Height =[2,1,5,6,2,3],
Return10.

Address: https://oj.leetcode.com/problems/largest-rectangle-in-histogram/

Algorithm: calculate the maximum rectangular area with the height of each bar as the width. The largest area is the largest area. You can find the subscript of the first bar lower than this bar on the left, as left_index, right find the subscript of the first bar lower than this bar, as right_index, the bar corresponds to the rectangular area = H [I] * (right_index-left_index ). The stack can be used to calculate the time complexity to O (n ). First, the condition for Stack entry is that the height of the empty stack or bar currently traversed is higher than that of the top of the stack. If the appeal condition is not met, the stack is released. This ensures that the node currently traversed must be the left_index of the stack element, and the next node of the stack element is right_index. Code:

 1 class Solution { 2 public: 3     int largestRectangleArea(vector<int> &height) { 4         int len = height.size(); 5         if(len < 1) return 0; 6         stack<int> stk; 7         int i = 0; 8         int max_area = 0; 9         while(i < len){10             if(stk.empty() || height[stk.top()] <= height[i]){11                 stk.push(i++);12             }else{13                 int t = stk.top();14                 stk.pop();15                 int area = height[t] * (stk.empty() ? i : i - stk.top() - 1);16                 if(area > max_area){17                     max_area = area;18                 }19             }20         }21         while(!stk.empty()){22             int t = stk.top();23             stk.pop();24             int area = height[t] * (stk.empty() ? len : len - stk.top() - 1);25             if(area > max_area){26                 max_area = area;27             }28         }29         return max_area;30     }31 };

 

Leetcode: largest rectangle in Histogram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.