LeetCode 84. Largest Rectangle in Histogram

Solution1:我的答案
循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!!!运行至第37个case时超时了….
还是记录一下。。。

class Solution {
public:int largestRectangleArea(vector<int>& heights) {int n = heights.size();if (n == 0) return 0;else if (n == 1) return heights[0];int height_max = INT_MIN;set<int> res;for (int i = 0; i < heights.size(); i++) height_max = max(height_max, heights[i]);for (int i = 0; i <= height_max; i++) {int temp = 0, temp_max = INT_MIN;vector<int> dp(n, 0);for (int j = 0; j < n; j++) {//DP,思路与在0、1组成的字符串里求最长1的串思路相同if (heights[j] < i)temp = 0;else {temp += i;dp[j] = temp;}temp_max = max(temp_max, dp[j]);}res.insert(temp_max);}return *(--res.end());}
};

Solution2:
参考网址:http://www.cnblogs.com/grandyang/p/4322653.html
每找到一个局部峰值,然后向前遍历所有的值,算出共同的矩形面积,每次对比保留最大值。
相当于只在每个峰值和下坡路上遍历,按理说复杂度是O(n2)O(n2)O(n^2)。但是确实很快而且可以AC。。。

class Solution {
public:int largestRectangleArea(vector<int>& height) {int res = 0;for (int i = 0; i < height.size(); ++i) {if (i + 1 < height.size() && height[i] <= height[i + 1]) {//若把这个判断条件去掉,则是暴力算法复杂度是O(n^2),只有最后一个case无法ACcontinue;}int minH = height[i];for (int j = i; j >= 0; --j) {minH = min(minH, height[j]);int area = minH * (i - j + 1);res = max(res, area);}}return res;}
};

正如注释中所说,此算法和暴力的时间复杂度相同,而暴力算法也只是最后一个case超时而已,不过只在峰值和下坡路上遍历确实很适合AC最后一个case…

LeetCode 84. Largest Rectangle in Histogram相关推荐

  1. [leetcode]84. Largest Rectangle in Histogram c语言

    题目 Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  2. leetcode 84. Largest Rectangle in Histogram | 84. 柱状图中最大的矩形(单调栈)

    题目 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题解 一句话总结:遍历数组,对于每个height[i],以其自身的高度 ...

  3. 84. Largest Rectangle in Histogram

    Title 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

  4. LeetCode hard 84. Largest Rectangle in Histogram--python,java 15行,c++ 15行 解法

    题目地址: Given n non-negative integers representing the histogram's bar height where the width of each ...

  5. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  6. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  7. 84直方图最大矩形覆盖 · Largest Rectangle in Histogram

    [抄题]: Given n non-negative integers representing the histogram's bar height where the width of each ...

  8. LeetCode84 Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  9. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

最新文章

  1. CSP认证202012-2 期末预测之最佳阈值[C++题解]:遍历、前缀和
  2. iOS - OC 面向对象语法
  3. 晴天php下载,x.php · 一步晴天/smart - Gitee.com
  4. 软工大二下半年第四周学习进度
  5. 深度比较Java循环的性能
  6. DefaultKeyedVector和KeyedVector用法
  7. 灰度重采样的方法分为_遥感导论-期末试卷及答案
  8. 使用jquery为table动态添加行的实现代码
  9. 团队-科学计算器-代码设计规范
  10. Servlet(10)—请求转发和请求重定向
  11. 测试用例之黑盒测试方法
  12. C语言 打印乘法口诀表
  13. 线性查找与二分查找的平均查找次数的比较
  14. wps页眉怎么设置不同页码_wps版word怎么从第二页设置页眉页脚
  15. lbj学习日记 04 循环和选择结构的刷题心得
  16. pollard_rho
  17. 输入行数,输出一个字母回文金字塔(c语言)
  18. 大学c语言基础 Turbo C2.0 (32位/64位通用)
  19. Java实现注册邮箱激活验证
  20. java给图片加水印_java中怎么样将水印加在图片的上面或者下面

热门文章

  1. mac os x 10.7下配置svn服务器
  2. linux文件内核目录,Linux内核目录文件简介
  3. java encode乱码_java 中文乱码问题的解决
  4. 系统在此应用程序中检测到基于堆栈的缓冲区_Linux 中的零拷贝技术
  5. pycharm去除波浪线的方法
  6. 自学Java要不要背熟语法_JAVASE经典面试问题(必须熟背),你Get到了吗?
  7. Python核心编程朱红庆_Python编程语言的核心是什么?
  8. 框架下cookie的使用_aspnetcore自带cookie的认证期限分析
  9. Python反向列表
  10. Java中的LinkedHashSet