85. Maximal Rectangle 由1拼出的最大矩形
[抄题]:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
二为矩阵:2个0,一个null
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
stack:把长度最长的列号暂存,然后取出来进行面积的比较
[一句话思路]:
新h[i]比旧h[i]长才能进,等于也行.
随着i的递增,旧h[i]比新h[i]长才用比较面积
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- h[]数组表示的是纵向长度,里面的index应该是横向坐标 cLen。多开辟一列 并且初始化为0,用于POP stack中之前的元素
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
stack中只存最之前和现在最长的
[复杂度]:Time complexity: O(n^2) Space complexity: O(n)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
dp is 2 hard
[Follow Up]:
[LC给出的题目变变变]:
84 histogram
[代码风格] :
class Solution {
public int maximalRectangle(char[][] matrix) {
//cc
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; //ini: cLen, rLen, Stack : for longest index, h[rLen + 1]
int rLen = matrix.length, cLen = matrix[0].length, max = 0;
int[] h = new int[cLen + 1];
h[cLen] = 0; //for loop: row (new stack) * col < cLen + 1
for (int row = 0; row < rLen; row++) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < cLen + 1; i++) {
//store h[i]
if (i < cLen)
if (matrix[row][i] == '1') h[i] += 1;
else h[i] = 0; //store i, compare area, add i to stack again
if (stack.isEmpty() || h[i] >= h[stack.peek()])
//新比旧长才能进,等于也行
stack.push(i); else {
while (!stack.isEmpty() && h[i] < h[stack.peek()]) {//旧比新长才用比较
int top = stack.pop();
int area = h[top] * (stack.isEmpty() ? i : i - stack.peek() - 1);
max = Math.max(max, area);
}
stack.push(i);
}
}
} return max;
}
}
85. Maximal Rectangle 由1拼出的最大矩形的更多相关文章
- 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
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 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 [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
随机推荐
- Python——深浅Copy
1. 赋值 赋值:指向同一块内存地址,所以同时改变 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3 ...
- Python笔试面试题_牛客(待完善)
中文,免费,零起点,完整示例,基于最新的Python 3版本.https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42 ...
- 2DAY初识python
一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...
- .NET单点登录实现方法----两种
第一种模式:同一顶级域名下cookie共享,代码如下 HttpCookie cookies = new HttpCookie("Token"); cookies.Expires = ...
- uboot环境变量的设置(未完待续)
使用print打印当前系统环境变量. 1. SMDK2440 # print baudrate=115200 bootargs=noinitrd root=/dev/nfs nfsroot=192.1 ...
- 【UVALive】2678 Subsequence(尺取法)
题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...
- jdk ssl证书
- 匿名方法,lambad表达式,匿名类
其实lambad表达式就是“函数”或者说是“方法”写法的一个进化,越来越简化而已,如数学方法里的f(X). 匿名方法:顾名思义,匿名方法就是没有名称的方法,但是有定义参数. 匿名方法最明显的好处就是可 ...
- Java Thread 多线程同步、锁、通信
参看:http://www.cnblogs.com/hoojo/archive/2011/05/05/2038101.html
- 一些通用的触发移动App崩溃的测试场景
一些通用的触发移动App崩溃的测试场景,如下: 1 验证在有不同的屏幕分辨率,操作系统和运营商的多个设备上的App行为. 2 用新发布的操作系统版本验证App的行为. 3 验证在如隧道,电梯等网络质量 ...