85.Maximal Rectangle---dp
题目链接:https://leetcode.com/problems/maximal-rectangle/description/
题目大意:给出一个二维矩阵,计算最大的矩形面积(矩形由1组成)。例子如下:

法一:将每一行的数据都看成是一个直方图,而每个直方图的高度都是由上一行得到的,例如,上述例子中,从上到下直方图的高度依次是:[1,0,1,0,0],[2,0,2,1,1],[3,1,3,2,2],[4,0,0,3,0]。也就是当当前值是0时,则高度是0;当前值是1时,则高度=上一行的高度+1。这样,对于每一行的直方图可以利用84题的求解办法,这里多的步骤就是将每一行数据转换成直方图,然后再根据每个直方图求解最大矩形面积。代码如下(耗时49ms):
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0) {
return 0;
}
Stack<Integer> s = new Stack<Integer>();
int h[] = new int[matrix[0].length];
int res = 0;
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
//转换成直方图
h[j] = (matrix[i][j] == '1') ? (h[j] + 1) : 0;
//根据每个直方图,计算其最大矩形面积,利用84题的方法
while(!s.isEmpty() && h[s.peek()] >= h[j]) {
int cur = s.pop();
res = Math.max(res, h[cur] * (s.isEmpty() ? j : (j - s.peek() - 1)));
}
s.push(j);
}
while(!s.isEmpty()) {
int cur = s.pop();
res = Math.max(res, h[cur] * (s.isEmpty() ? h.length : (h.length - s.peek() - 1)));
}
}
return res;
}
法二:dp思想,依旧将每一行的数据看成一个直方图,然后转换成直方图,用left[]数组表示左边界1的位置,用right[]数组表示右边界1的位置。代码如下(耗时13ms):
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0) {
return 0;
}
int h[] = new int[matrix[0].length];
int left[] = new int[matrix[0].length], right[] = new int[matrix[0].length];
//初始化right数组 为matrix[0].length
for(int i = 0; i < matrix[0].length; i++) {
right[i] = matrix[0].length;
}
int res = 0;
for(int i = 0; i < matrix.length; i++) {
int cur_left = 0, cur_right = matrix[0].length;
//转换成直方图
for(int j = 0; j < matrix[0].length; j++) {
h[j] = (matrix[i][j] == '1') ? (h[j] + 1) : 0;
}
//计算左边1的下标
for(int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j] == '1') {
left[j] = Math.max(left[j], cur_left);
}
else {
left[j] = 0;
cur_left = j + 1;
}
}
//计算右边1的下标
for(int j = matrix[0].length - 1; j >= 0; j--) {
if(matrix[i][j] == '1') {
right[j] = Math.min(right[j], cur_right);
}
else {
right[j] = matrix[0].length;
cur_right = j;
}
}
//计算矩形面积
for(int j = 0; j < matrix[0].length; j++) {
res = Math.max(res, (right[j] - left[j]) * h[j]);
}
}
return res;
}
85.Maximal Rectangle---dp的更多相关文章
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 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 ...
随机推荐
- Oracle 10g DG 环境搭建详解
环境描述:线上招聘库在物理机上,需要上云主机,于是申请两台云主机:由于云主机变态性,分配的云主机具有很多局限性:1.没有/tmp,2.没有 swap 3. /home 目录非常小:于是申请两块云硬盘, ...
- Liunx 和 Win中的软链接详解
用过Linux的朋友都知道linux中有软链接的概念,可以通过ln命令创建到目录或文件的软链接,软链接的好处就是可以让一个目录或文件有多个入口但保持单一物理位置,方便应用和管理. 1.命令格式: ...
- 【数据库_Mysql】MySQL—修改表时给表添加联合主键约束
添加语法如下: “ALTER TABLE table_name ADD CONSTRAINT pk_table_name PRIMARY KEY(列名1,列名2):” [示例1]假设订房信息表(O ...
- 【转】ssh登录原理以及ssh免密码登陆
一.什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会 ...
- Jsp遍历后台传过来的List
1:使用jstl标签 (可以和自定义标签配合使用) 首先引用jstl标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" ...
- BZOJ2716:[Violet 3]天使玩偶——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2716 样例输入 2 31 12 32 1 21 3 32 4 2 样例输出 1 2 ———————— ...
- NOI2013 矩阵游戏 【数论】
题目描述 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i ...
- SQL注入(SQL Injection)案例和防御方案
sql注入(SQL Injection):就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. SQL注入攻击的主要危害包括:非法读取.篡 ...
- Codeforces 97.B Superset
A set of points on a plane is called good, if for any two points at least one of the three condition ...
- Qt ------ Thread Affinity (QObject::moveToThread: Cannot move objects with a parent)
简单的说,每个QObject的对象,都和某个创建对象所在的线程关联,如果把对象通过 moveToThread 移动到其他线程,这个对象不能有父对象,否则会出现 QObject::moveToThrea ...