84. Largest Rectangle in Histogram Hard
来源:互联网 发布:淘宝客服主要负责什么 编辑:程序博客网 时间:2024/05/21 08:40
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.
思路:本体最直接的想法就是遍历每一种情况,通过比较来获得矩形的最大值。如何更快地获得各种情况,我们先从特殊情况考虑,假设所有的方阵高度是递增的,那么我们只需要考虑每一个方阵到最右端方阵所形成的矩形大小就可以了。我们就构造一种递增的序列来求得以每一个方阵为高的矩形面积。
网上有人想到了使用栈的方式。将高较小的均放入栈中,一旦遇见高大的,就出栈并按照递增方式的面积求法得到一个面积,取最大值,将我们现在正在比较的方阵入栈,注意入栈时应该是出栈了多少个就该入栈几次正在比较的方阵,这样是为了自求面积是获得以此方阵为高的矩形的最大面积。
class Solution {public: int largestRectangleArea(vector<int>& heights) { int area = 0; stack<int>temp; for (int i = 0; i < heights.size(); i++) { if (temp.empty() || temp.top() <= heights[i]) { temp.push(heights[i]); } else { int k = 0; while (!temp.empty() && temp.top() > heights[i]) { k++; area = max(area, temp.top() * k); temp.pop(); } while (k--) { temp.push(heights[i]); } temp.push(heights[i]); } } int k = 1; while (!temp.empty()) { area = max(area, temp.top() * k); k++; temp.pop(); } return area; }};
0 0
- 84. Largest Rectangle in Histogram[hard]
- 84. Largest Rectangle in Histogram Hard
- Leetcode题解14 84. Largest Rectangle in Histogram(hard)
- Hard-题目27:84. Largest Rectangle in Histogram
- Leetcode 84. Largest Rectangle in Histogram (Hard) (cpp)
- LeetCode 84. Largest Rectangle in Histogram(Hard)
- leetcode hard专杀之84. Largest Rectangle in Histogram
- 84、Largest Rectangle in Histogram(Hard)
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. largest rectangle in histogram
- 84. Largest Rectangle in Histogram
- 84.Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 文章标题 C++成员变量以及成员函数指针
- 单击按钮打开一个对话框
- 玲珑杯 1101
- 作用域
- 文件查找命令
- 84. Largest Rectangle in Histogram Hard
- 设计模式笔记二十二:空对象模式
- HTTP状态码大全
- [课设]日常蠢哭自己
- apache的配置文件和相关说明
- 百度前端学院任务笔记(二)
- SpringMVC学习(三)之@RequestParam&@RequestHeader&@CookieValue
- C++之多态(静态多态+动态多态+虚函数+虚析构函数+多态原理[未完待续])
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>