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的更多相关文章

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

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

  2. 刷题84. Largest Rectangle in Histogram

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

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

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

  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. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

    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

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

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

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

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

随机推荐

  1. asp.net mvc5轻松实现插件式开发

    在研究Nopcommece项目代码的时候,发现Nop.Admin是作为独立项目开发的,但是部署的时候却是合在一起的,感觉挺好 这里把他这个部分单独抽离出来, 主要关键点: 确保你的项目是MVC5 而不 ...

  2. IdentityServer4关于多客户端和API的最佳实践【含多类型客户端和API资源,以及客户端分组实践】【下】

    经过前两篇文章你已经知道了关于服务器搭建和客户端接入相关的基本资料,本文主要讲述整个授权系统所服务的对象,以ProtectApi资源为演示 目标: 1)实现多资源服务器针对请求的token校验,接入I ...

  3. Java--实现单点登录

    1 什么是单点登陆 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用 ...

  4. Spring基于纯注解方式的使用

    经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...

  5. 《移山之道:VSTS软件开发指南》读书笔记

    这两天看了<移山之道:VSTS软件开发指南>,对团队软件开发又有了新的认识.也许对于我们这些软件开发的新手来说,最重要的是具体技术与应用框架,但读了这本书后我感觉到,实际团队项目中工具的使 ...

  6. sql语句中left join和inner join中的on与where的区别分析

    关于SQL SERVER的表联接查询INNER JOIN .LEFT JOIN和RIGHT JOIN,经常会用到ON和WHERE的条件查询,以前用的时候有时是凭感觉的,总是没有搞清楚,今日亲自测试了下 ...

  7. Python简单的网络编程

    OSI 模型介绍 应用层 -- 对接受的数据进行解释.加密与解密.压缩与解压缩 会话层 -- 通过传输层(端口号: 传输端口和接受端口) 建立数据传输的通路 传输层 -- 定义了一些传输数据的协议和端 ...

  8. php soapclient 超时 设置

    用php的soapclient,默认是60秒.可在php.ini里配置, 重启APache 或者在PHP代码里做设置 ini_set('default_socket_timeout', 300);// ...

  9. Android--px(像素)和dp、sp之间的相互转化

    public class DensityUtil { public DensityUtil() { } public static int dip2px(Context var0, float var ...

  10. Azure Ubuntu18.04安装lxde桌面记录,Windows远程连接Ubuntu18.04(Linux)

    执行如下命令: 尽量按以下顺序执行,否则可能会发生意向不到的问题(坑) 1.更新数据源 sudo apt-get update 2.更新安装包 sudo apt-get upgrade 3.安装lxd ...