Question

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 rectangle is shown in the shaded area, which has area = 10 unit.

For example,

Given heights = [2,1,5,6,2,3],

return 10.

Solution 1

动态规划,但是会超时。

Code 1

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int num = heights.size();
if (num == 0)
return 0;
vector<vector<int>> min_heights(num, vector<int>(num, 0));
vector<vector<int>> max_areas(num, vector<int>(num, 0));
for (int i = 0; i < num; i++) {
min_heights[i][i] = heights[i];
max_areas[i][i] = heights[i] * 1;
}
for (int i = 2; i <= num; i++) {
for (int j = 0; j <= num - i; j++) {
min_heights[j][j + i - 1] = min(heights[j], min_heights[j + 1][j + i - 1]);
max_areas[j][j + i - 1] = max(min_heights[j][j + i - 1] * i, max_areas[j + 1][j + i - 1]);
max_areas[j][j + i - 1] = max(max_areas[j][j + i - 1], max_areas[j][j + i - 2]);
}
} return max_areas[0][num - 1];
}
};

Solution 2

同样是动态规划,但是这次只需要记录之前的比自己低的坐标值。然后遍历之前比自己低的坐标值,每个低的都计算一次面积,具体看代码实现。

Code 2

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// 记录比自己低的index
int res = 0;
height.push_back(0);
vector<int> index;
for (int i = 0; i < height.size(); i++) {
// 遍历所有比当前高度高的值,这也就是为什么会在height后面插入0的原因
while (index.size() > 0 && height[index.back()] >= height[i]) {
int h = height[index.back()];
index.pop_back(); // 这个是为了计算宽度值
int idx = index.size() > 0 ? index.back() : -1;
if (h * (i - idx - 1) > res)
res = h * (i - idx - 1);
}
index.push_back(i);
}
return res;
}
};

LeetCode——Largest Rectangle in Histogram的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  3. LeetCode: Largest Rectangle in Histogram 解题报告

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

  4. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

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

  5. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  6. [leetcode]Largest Rectangle in Histogram @ Python

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

  7. [LeetCode] Largest Rectangle in Histogram

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

  8. leetcode -- Largest Rectangle in Histogram TODO O(N)

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

  9. [LeetCode] Largest Rectangle in Histogram 解题思路

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

随机推荐

  1. 通过js获取UserAgent写入数据库 js传值至php

    借助cookie,cookie是js和php互相传值的纽带.

  2. 网络编程(基于udp协议的套接字/socketserver模块/进程简介)

    一.基于UDP协议的套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议. 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号,就 ...

  3. Mysql中字段类型之时间戳大坑

         一 .环境说明: 在目前项目中,有这样的一张表,用来记录会议的相关信息,例如:会议的内容.会议的参会人员.会议的地点.会议的状态(会议是否已结束.会议是否被撤销).会议的开始时间以及该条信息 ...

  4. python yield的终极解释

    (译)Python关键字yield的解释(stackoverflow): http://stackoverflow.com/questions/231767/the-python-yield-keyw ...

  5. linux下python编辑器的tab补全

    vi tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcompleter ...

  6. HTML5笔记——formData

    注:formData中的数据在控制台上的console里面是打印不出来的,只能在控制台的network里面查看到具体的发送数据和发送选项 文章出处:梦想天空 XMLHttpRequest Level ...

  7. day5-正则表达式 re

    re模块用于对python的正则表达式的操作. 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配 ...

  8. Creating an AVI in memory with C++

    Creating an AVI in memory with C++        The following example demonstrates how an avi file is comp ...

  9. PHP压缩与解压Zip(PHPZip类)

    <?php     class PHPZip     {         private $ctrl_dir     = array();         private $datasec    ...

  10. 在Delphi中使用indy SMTP发送gmail邮件[转]

    在Delphi中使用indy SMTP发送gmail邮件[转] 2012-01-01 22:44:30|  分类: Delphi |  标签: |举报 |字号大中小 订阅     在Delphi中发送 ...