Leetcode 之Largest Rectangle in Histogram(40)

又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈,O(n)就能解决问题,其中的过程值得细细体会。

int largestRect(vector<int> &height)
      {
          stack<int> s;//定义一个单调递增栈
          height.push_back(0);//定义单调递增栈的最后一个
          int result = 0;//记录当前最大的面积
          for (int i = 0; i < height.size();)//满足条件i才递增
          {
              if (s.empty() || height[i]>s.top())
              {
                  s.push(i++);//只有当前元素大于栈顶元素是才入栈相应的序号,构造递增栈
              }
              else {
                  //当前元素小于栈顶元素时
                  int tmp = s.top();//保留栈顶元素
                  s.pop();//弹出直至当前元素大于栈顶元素,使栈仍然是递增的
                  //
                  result = max(result, height[tmp]*(s.empty() ? i : i - s.top() - 1));
              }
          }

          return result;
      }

时间: 2024-11-05 20:43:00

Leetcode 之Largest Rectangle in Histogram(40)的相关文章

[LeetCode OJ] 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

C++]LeetCode: 133 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 larg

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

leetcode 84 Largest Rectangle in Histogram ----- java

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

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Histogram第四种思路,或者翻译版Largest Rectangle in [email protected],JAVA实现如下: public int largestRectangleArea(int[] height) { Stack<Integer> stk = new Stack<In

LeetCode之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. For example,Given height = [2,1,5,6,2,3],return 10. 题目的大意即给出一个柱状图的歌数据,求柱状图内所包含的最大矩形. 这道

[leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

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

[LeetCode][Java] 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]. For exam

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

19.2.22 [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