{"id":6313,"date":"2020-02-14T00:14:24","date_gmt":"2020-02-14T08:14:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6313"},"modified":"2020-02-15T09:14:48","modified_gmt":"2020-02-15T17:14:48","slug":"leetcode-84-largest-rectangle-in-histogram","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-84-largest-rectangle-in-histogram\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 84. Largest Rectangle in Histogram"},"content":{"rendered":"\n<p>Given&nbsp;<em>n<\/em>&nbsp;non-negative integers representing the histogram&#8217;s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/10\/12\/histogram.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><br>Above is a histogram where width of each bar is 1, given height =&nbsp;<code>[2,1,5,6,2,3]<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/10\/12\/histogram_area.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><br>The largest rectangle is shown in the shaded area, which has area =&nbsp;<code>10<\/code>&nbsp;unit.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> [2,1,5,6,2,3]\n<strong>Output:<\/strong> 10<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Monotonic Stack<\/strong><\/h2>\n\n\n\n<p>Use a monotonic stack to maintain the higher bars&#8217;s indices in ascending order.<br>When encounter a lower bar, pop the tallest bar and use it as the bottleneck to compute the area.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 12ms, 10.7MB\nclass Solution {\npublic:\n  int largestRectangleArea(vector<int>& heights) {\n    heights.push_back(0);\n    const int n = heights.size();\n    stack<int> s;\n    int ans = 0;\n    int i = 0;\n    while (i < n) {\n      if (s.empty() || heights[i] >= heights[s.top()]) {\n        s.push(i++);\n      } else {\n        int h = heights[s.top()]; s.pop();\n        int w = s.empty() ? i : i - s.top() - 1;        \n        ans = max(ans, h * w);\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;n&nbsp;non-negative integers representing the histogram&#8217;s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[217,552,180],"class_list":["post-6313","post","type-post","status-publish","format-standard","hentry","category-stack","tag-hard","tag-monotonic-stack","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6313"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6313\/revisions"}],"predecessor-version":[{"id":6315,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6313\/revisions\/6315"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}