一、题目说明

题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积。题目难度是Hard!

二、我的解答

这是一个 看起来容易,做起来很容易错的题目。我开始用的是“挖坑法”,遗憾的是总是Time Limit Exceeded。经过10次优化,还是很难看。

  1. class Solution{
  2. public:
  3. int largestRectangleArea(vector<int>& heights){
  4. int len = heights.size();
  5. if(len<1) return 0;
  6. if(len==1) return heights[0];
  7. int result = 0;
  8. int start=len-1,end=0;
  9. int cnt = 0,sum=0;
  10. int min;
  11. while(1){
  12. min = INT_MAX;
  13. for(int i=0;i<len;i++){
  14. if(heights[i]>0 && heights[i]<min){
  15. min = heights[i];
  16. }
  17. }
  18. //找到第1个正的
  19. while(end<len && heights[end]<=0){
  20. end++;
  21. }
  22. while(start>0 && heights[start]<=0){
  23. start--;
  24. }
  25. if(start==end){
  26. sum = heights[start];
  27. if(sum>result) result = sum;
  28. break;
  29. }else if(end>start) {
  30. break;
  31. }
  32. sum = 0;
  33. cnt = 0;
  34. //一次遍历,求大于0的连续长度 最大值
  35. while(end<len){
  36. cnt = 0;
  37. sum = 0;
  38. while(end<len && heights[end]>=min){
  39. if(heights[end]==min) heights[end] = 0;
  40. cnt++;
  41. end++;
  42. }
  43. sum = cnt * min;
  44. if(sum>result) result = sum;
  45. if(end<len) end++;
  46. }
  47. end = 0;
  48. start = len-1;
  49. }
  50. return result;
  51. }
  52. };

性能:

  1. Runtime: 1536 ms, faster than 4.53% of C++ online submissions for Largest Rectangle in Histogram.
  2. Memory Usage: 9.9 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.

还能想到的方法就是暴力计算法,性能也差不多:

  1. class Solution{
  2. public:
  3. //brute force
  4. int largestRectangleArea(vector<int>& heights){
  5. int len = heights.size();
  6. if(len<1) return 0;
  7. if(len==1) return heights[0];
  8. int result = 0;
  9. bool allthesame = true;
  10. for(int i=0;i<len-1;i++){
  11. if(heights[i]!=heights[i+1]){
  12. allthesame = false;
  13. }
  14. }
  15. if(allthesame){
  16. return heights[0]*len;
  17. }
  18. for(int i=0;i<len;i++){
  19. int minHeight = INT_MAX;
  20. for(int j=i;j<len;j++){
  21. minHeight = min(minHeight,heights[j]);
  22. result = max(result,minHeight * (j-i+1));
  23. }
  24. }
  25. return result;
  26. }
  27. };
  1. Runtime: 1040 ms, faster than 5.03% of C++ online submissions for Largest Rectangle in Histogram.
  2. Memory Usage: 10 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.

三、优化措施

用单调递增stack法,代码如下:

  1. class Solution{
  2. public:
  3. //单调递增栈
  4. int largestRectangleArea(vector<int>& heights){
  5. int result = 0;
  6. stack<int> st;
  7. st.push(-1);//-1 放进栈的顶部来表示开始
  8. //按照从左到右的顺序,我们不断将柱子的序号放进栈中,直到 heights[i]<heights[st.top]
  9. //将栈中的序号弹出,直到heights[stack[j]]≤heights[i]
  10. for(int i=0;i<heights.size();i++){
  11. while(st.top()!=-1 && heights[i]<heights[st.top()]){
  12. int h = st.top();
  13. st.pop();
  14. result = max(result,heights[h]*(i - st.top() -1));
  15. }
  16. st.push(i);
  17. }
  18. // 遍历完了,但是没计算完
  19. while(st.top() != -1){
  20. int h = st.top();
  21. st.pop();
  22. int len = heights.size() - st.top() -1;
  23. result = max(result,heights[h]*len);
  24. }
  25. return result;
  26. }
  27. };
  1. Runtime: 16 ms, faster than 53.51% of C++ online submissions for Largest Rectangle in Histogram.
  2. Memory Usage: 10.4 MB, less than 91.43% of C++ online submissions for Largest Rectangle in Histogram.

继续优化:

  1. class Solution{
  2. public:
  3. //单调递增栈 ,借用i当栈
  4. int largestRectangleArea(vector<int>& heights){
  5. int result = 0;
  6. int len, wid;
  7. for (int i = 0; i < heights.size(); i++) {
  8. if(i != heights.size() - 1 && heights[i] <= heights[i + 1]) continue; //这一步的判断很玄妙
  9. wid = heights[i];
  10. for (int j = i; j >= 0; j--) {
  11. len = i - j + 1;
  12. wid = min(wid, heights[j]);
  13. result = max(result, len * wid);
  14. }
  15. }
  16. return result;
  17. }
  18. };
  1. Runtime: 12 ms, faster than 89.13% of C++ online submissions for Largest Rectangle in Histogram.
  2. Memory Usage: 10 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.
  3. Next challenges:

刷题84. Largest Rectangle in Histogram的更多相关文章

  1. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  2. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  3. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  4. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

  6. LeetCode OJ 84. Largest Rectangle in Histogram

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

  7. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

  8. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

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

  9. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

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

随机推荐

  1. python3 密码字典匹配wifi密码,有界面

    界面: 需要先安装一下pywifi模块 代码: # coding:utf-8 from tkinter import * from tkinter import ttk import pywifi f ...

  2. 19_07_07校内训练[xor]

    题意 长度为n的数组,上面有k个位置是1,现在有l种长度的连续全1串,要求用最少的次数将这个数组异或成全0的数组.n<=1E5,k<=10,l<=100. 思考 先将数组进行异或的差 ...

  3. linux下误清用户/home下的文件怎么办?

    2016-08-19 10:38:10   有时候我们不小心把home目录下的用户目录删除了,出现上图情况,每行开头直接变成-bash-3.2$这种形式而不是[lyp@centos7 ~]$这种,这时 ...

  4. 工具之sort

    转自:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参 ...

  5. mac电脑下使用fcrackzip破解zip压缩文件密码

    fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破解速度快,能使用字典和指定字符集破解,适用于linux.mac osx 系统 fcrackzip安 ...

  6. geojson转esriJson

    因为一些特殊需求,需要将geojson转为shp数据,网上有一些转换网站,但是存在一些问题,例如中文乱码.文件大小限制等等,折腾了一下,还是觉得用arcgis转比较好,因此先将geojson转为esr ...

  7. Word文档分节设置页码

    在一篇论文中需要将摘要和目录作为一部分设置罗马数字页码,正文部分设置阿拉伯数字页码. 大致效果如下图所示: 这里面用到了分节符,步骤如下: 1 :点击开始菜单栏下 显示/隐藏编辑标记 2:点击插入菜单 ...

  8. 手把手实操教程!使用k3s运行轻量级VM

    前 言 k3s作为轻量级的Kubernetes发行版,运行容器是基本功能.VM的管理原本是IaaS平台的基本能力,随着Kubernetes的不断发展,VM也可以纳入其管理体系.结合Container和 ...

  9. CCF_ 201312-3_最大的矩形

    遍历数组中每一元素,左右延伸得出宽度. #include<iostream> #include<cstdio> using namespace std; int main() ...

  10. 《剑指Offer》第二章(一)题 9 -12

    第二章 面试题9:用两个栈实现队列 题目:如面试题,给你两个栈, 实现队列的先进先出,即在队列头删除一个元素以及在队列的尾部添加一个元素 思路:这个题的分析感觉很巧妙,从一个具体的例子入手,找出其中的 ...