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 ...
随机推荐
- [Code Festival 2017 qual A] C: Palindromic Matrix
题意 给出一个小写字母组成的字符矩阵,问能否通过重排其中的字符使得每行每列都是回文串. 分析 简化版:给出一个字符串,问能否通过重排其中的字符使得它是回文串.那么如果字符串长度为偶数,就需要a到z的个 ...
- DelayQueue实现Java延时任务
最近公司需要实现一个订单超时自动关闭的功能,由Java这块来实现 一开始我以为就是定时任务,深入了解了之后发现并不是,官方名称应该叫延时任务,到时间之后 执行传过来的回调函数 这个功能我一共前前后后写 ...
- 【BZOJ4137】火星商店问题(线段树分治,可持久化Trie)
[BZOJ4137]火星商店问题(线段树分治,可持久化Trie) 题面 洛谷 BZOJ权限题 题解 显然可以树套树,外层线段树,内层可持久化Trie来做. 所以我们需要更加优美的做法.--线段树分治. ...
- 洛谷 P3312 [SDOI2014]数表 解题报告
P3312 [SDOI2014]数表 题目描述 有一张\(N*M\)的数表,其第\(i\)行第\(j\)列(\(1\le i \le n\),\(1 \le j \le m\))的数值为能同时整除\( ...
- nginx服务器去掉url中的index.php 和 配置path_info
隐藏index.php server { listen 80; server_name yourdomain.com; root /home/yourdomain/www/; index index. ...
- bzoj4753: [Jsoi2016]最佳团体(分数规划+树形依赖背包)
菜菜推荐的“水题”虐了我一天T T...(菜菜好强强qwq~ 显然是个分数规划题,二分答案算出p[i]-mid*s[i]之后在树上跑依赖背包,选k个最大值如果>0说明还有更优解. 第一次接触树形 ...
- 解题:由乃OI 2018 五彩斑斓的世界
题面 写在前面的扯淡: 分块的总体学习告一段落,这算是分块集中学习的最后一题么:以后当然也可能会写,就是零零散散的题了=.= 在洛谷上搜ynoi发现好像只有这道题和 由乃OI 2018 未来日记 是分 ...
- codeforces 217E 【Alien DNA】
倒序考虑每一个操作,对于一个操作$[l, r]$,他产生的影响区间将是$[r+1,r + r + l - 1]$,如果$r+l-1>K$的话,$K$之后的区间我们是不关心的. 暴力扫描这个区间 ...
- PID控制算法的C语言实现十一 模糊算法简介
在PID控制算法的C语言实现九中,文章已经对模糊PID的实质做了一个简要说明.本来打算等到完成毕业设计,工作稳定了再着力完成剩下的部分.鉴于网友的要求和信任,抽出时间来,对模糊PID做一个较为详细的论 ...
- 011. C++ friend使用
1.friend 友元 将一个函数定义为friend,可以读取private数据: 显然,friend提供便利的同时,会破坏C++的封装性,因此,建议谨慎使用,朋友多了也许是个困扰. class co ...