84. Largest Rectangle in Histogram
84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html
整体思路是递增不处理,当遇到减少时,计算之前所有大于当前高度的最优解。因为实际上只要遇到比你小的,就不可能以你为高度了。索引之间的差刚好能反应当前栈中高度所覆盖的区域,即宽度。
维护一个递增的栈,每次只用以当前栈的index的相对高 * 宽度,宽度就是以当前栈的index的右侧。每次计算的是一个局部最优解。
i--的目的是让比较对象一直停留在这个位置;
push 0的目的是计算所有的栈中剩下的大小;
当前比较的i,实际上只计算i左侧的最优解,所以是i - con.top() - 1,并且con.top()是递增的下一个index。
1.存储一个单调递增的栈
2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1
3.单调递增的栈,如果遇到比栈的top小的,就要计算一次。其实可以看到,这个top,从左向top是最大的,top向右也是最大的,所以只能乘以他自己。
注意:这个地方con必须pop(),这样才能找到当前top所属于的高度所覆盖的宽度,比如当前top的值是7,pop出来后stack的top的值可能变成3,覆盖的区域是4,因为stack里面不是3、4、5这样连续的,大概率是一些间隔的。这也就是为什么后面使用i - con.top() - 1 ,而不使用i - index。
每次当前位置的height比stack的top小的,就需要计算以stack顶部的height为高度的局部最优解。
算覆盖区域的时候,必是i - con.top(),这个时候你就应该考虑是否-1。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> con;
int area = ;
heights.push_back();
for(int i = ;i < heights.size();i++){
if(con.empty() || heights[con.top()] < heights[i])
con.push(i);
else{
int index = con.top();
con.pop();
area = max(area,heights[index] * (con.empty() ? i : (i - con.top() - )));
i--;
}
}
return area;
}
};
85. Maximal Rectangle
http://www.cnblogs.com/grandyang/p/4322667.html
把矩形分成一行一行送入子函数获得每行的最大值,然后再比较各行的最大值获得总的最大值。每行的计算和largest rectangle in histogram是一样的。
送入的每行不是0、1组成,而是每行的高度,这样就转换成了largest rectangle in histogram的问题
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
if(m <= )
return ;
int n = matrix[].size();
if(n <= )
return ;
int res = ;
vector<int> input(n);
for(int i = ;i < m;i++){
for(int j = ;j < n;j++)
input[j] = (matrix[i][j] == '' ? : + input[j]);
res = max(res,maximal(input));
}
return res;
}
int maximal(vector<int> matrix){
stack<int> s;
int res = ;
matrix.push_back();
for(int i = ;i < matrix.size();i++){
if(s.empty() || matrix[s.top()] < matrix[i])
s.push(i);
else{
int num = s.top();
s.pop();
res = max(res,matrix[num] * (s.empty() ? i : (i - s.top() - )));
i--;
}
}
return res;
}
};
221. Maximal Square
https://www.cnblogs.com/grandyang/p/4550604.html 解法3
以正方形的右下角进行判断,只用判断左上角,左边和上边。
dp[i][j] 表示到达 (i, j) 位置所能组成的最大正方形的边长。
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty())
return ;
if(matrix[].empty())
return ;
int res = ;
vector<vector<int> > area(matrix.size(),vector<int>(matrix[].size(),));
for(int i = ;i < matrix.size();i++){
for(int j = ;j < matrix[].size();j++){
if(i == || j == )
area[i][j] = matrix[i][j] - '';
else if(matrix[i][j] == '')
area[i][j] = min(area[i-][j-],min(area[i-][j],area[i][j-])) + ;
res = max(res,area[i][j]);
}
}
return res*res;
}
};
84. Largest Rectangle in Histogram的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 刷题84. Largest Rectangle in Histogram
一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 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 ...
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- 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 ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- jQuery 小特效【文本框折叠隐藏,展开显示】【下拉菜单】【颜色渐变】【弹窗+遮罩】
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- jquery 截取屏幕
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- HTML中令人惊喜的全局属性
1.accesskey 属性 : 规定激活元素的快捷键. 浏览器支持:几乎所有浏览器均 accesskey 属性,除了 Opera. 定义和用法 accesskey 属性规定激活(使元素获得焦点)元素 ...
- JS性能优化 之 文档片段 createDocumentFragment
我们用原生JS进行开发时,经常会用到两种更新DOM节点的方法:innerHTML 和 appendChild() .其中 innerHTML 会完全替换掉原先的节点内容,如果我们是想向元素追加子节点的 ...
- python 类函数,实例函数,静态函数
一.实现方法 class Function(object): # 在类定义中定义变量 cls_variable = "class varibale" def __init__(se ...
- Loadrunner 脚本开发-利用Loadrunner生成Web service测试脚本
脚本开发-利用Loadrunner生成Web service测试脚本 1.选择协议--Web Service,如下图 2.导入服务 入口1:点击Manage Services ->弹出窗中选择“ ...
- 安卓开发之ScrollView
当界面不足以将所有的内容显示出来的时候便导致下面的部分内容无法显示出来 所有加上ScrollView 来讲要显示的内容放入之中便可以实现上下滚动界面内容 但是当要显示多个控件的时候会出错 原因是Sc ...
- unicode编码和utf8编码的区别
编码格式不同在数据的传输和显示会有很大的影响.最近在使用的过程中发现一些网络文件 传输的编码格式问题,会影响文件的正常传输,于是查看了一下网上的资料,自己也写一篇 小总结. uicode是万国码,用1 ...
- 自己写bitmap
备注:1.原创文章,转载请标明出处: 2.欢迎建议和意见 3.我的实现是C语言,为了保护公司隐私,下述数据类型被我改了.实际上int应改是无符号4个字节的类型,byte是有符号1个字节,才能保证移植性 ...
- 理解inode 以及 软链接和硬链接概念区分
inode简单理解 本文来源自网络文章,并针对文章内容加以批注和修改.希望能帮到你! 一. 磁盘设备 说到inode,首先必须要提及下<操作系统>中磁盘存储器的管理一节.磁盘设备是一种相当 ...