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

For example, Given height = [2,1,5,6,2,3], return 10.

思路: 注意一点: 只要计算出以每个柱形为最小值的矩形面积即可。使用一个栈,栈内始终保存一个递增序列的 index,若是 新的柱形长度小于栈顶元素,则退出栈顶直到栈内元素的长度不大于新的柱形的长度为止,并且,对于每一个退栈元素,计算以其长度为最小值的面积。(宽的左边为其自身位置,右边为新到元素的位置)时间:O(n)

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int n = height.size(), max_area = 0;
int Id, area;
int i = 0;
stack<int> st; // save the index
while(i < n) {
if(st.empty() || height[i] >= height[st.top()]) st.push(i++);
else {
Id = st.top();
st.pop();
area = height[Id] * (st.empty() ? i : i - st.top() - 1);
if(area > max_area) max_area = area;
}
}
while(!st.empty()) {
Id = st.top();
st.pop();
area = height[Id] * (st.empty() ? i : i - st.top() - 1);
if(area > max_area) max_area = area;
}
return max_area;
}
};

Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

思路: 一行一行的记录下当前高度, 用上题的思路计算一下,即可。时间: O(n2)

int getMaxArea(vector<int> &h) {
stack<int> st;
int maxArea, i = 0;
while(i < h.size()) {
if(st.empty() || h[st.top()] <= h[i]) { st.push(i++); continue; }
while(!st.empty() && h[st.top()] > h[i]) {
int id = st.top();
st.pop();
int area = h[id] * (st.empty() ? i : i - st.top() - 1);
if(area > maxArea) maxArea = area;
}
}
while(!st.empty()) {
int id = st.top();
st.pop();
int area = h[id] * (st.empty() ? i : i - st.top() - 1);
if(area > maxArea) maxArea = area;
}
return maxArea;
}
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
int row = matrix.size(), col = matrix[0].size();
vector<int> h(row, 0);
int maxArea = 0;
for(int c = 0; c < col; ++c) {
for(int r = 0; r < row; ++r) {
if(matrix[r][c] == '1') ++h[r];
else h[r] = 0;
}
maxArea = max(maxArea, getMaxArea);
}
return maxArea;
}
};

47. Largest Rectangle in Histogram && Maximal Rectangle的更多相关文章

  1. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  2. leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle

    https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...

  3. 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 ...

  4. Maximal Rectangle&Largest Rectangle in Histogram

    这两天在做leetcode的题目,最大矩形的题目以前遇到很多次了,一直都是用最笨的方法,扫描每个柱子,变换宽度,计算矩形面积,一直都以为就这样O(n2)的方法了,没有想到居然还有研究出了O(n)的算法 ...

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

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

  6. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  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. 84. Largest Rectangle in Histogram

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

  9. [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. web.xml的初始化参数

    web.xml的初始化参数 ---------首先声明,这里所介绍的是web中context-param,init-param参数的初始化配置---------- ------------------ ...

  2. 个推推送iOS版 常见问题详解

    原文:http://www.oschina.net/question/1782938_234760   1.提交了.p12文件后多久可以测试? 提交后10分钟左右才可以测试,并不是立即生效的.   2 ...

  3. MySQL数据库忘记root密码解决办法

    MySQL数据库忘记root密码解决办法 1.在运行输入services.msc打开服务窗体,找到MYSQL服务.右键停止将其关闭.如图:

  4. JVM-并发-线程安全与锁优化

    线程安全与锁优化 1.线程安全 (1)当多个线程访问一个对象时,如果不考虑这些线程在执行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方进行任何其他的协调操作,调用这个对象的行为都可以获 ...

  5. c#中各类日期的计算方法,收藏

    DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一D ...

  6. linux命令:exec

    1.命令介绍: exec用来配合find命令找到的文件后接着执行相应的命令 2.命令格式: find . -type f exec ls -l {} \;

  7. Makefile 开发环境全能管家

    变量的应用: CC=gcc RM=rm EXE=main.exe OBJS=目标 伪目标的应用: .PHONY:clean 自动变量的应用: $@:表示一个规则的目标 $^:表示的是规则中的所有的先决 ...

  8. 阿里 RocketMQ 安装与简介

    一.简介 官方简介: l  RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: l  能够保证严格的消息顺序 l  提供丰富的消息拉取模式 l  高效的订阅者水平扩展能力 l  实时的 ...

  9. Python 利用pytesser模块识别图像文字

    使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the ...

  10. Kaiju: Fast and sensitive taxonomic classification for metagenomics

    Kaiju: Fast and sensitive taxonomic classification for  metagenomics   问题描述:However, nucleotide comp ...