Java实现 LeetCode 85 最大矩形
85. 最大矩形
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
示例:
输入:
[
[“1”,“0”,“1”,“0”,“0”],
[“1”,“0”,“1”,“1”,“1”],
[“1”,“1”,“1”,“1”,“1”],
[“1”,“0”,“0”,“1”,“0”]
]
输出: 6
PS:
使用单调栈方法求解(同84)
class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
int[] height = new int[matrix[0].length];
int globalmax = 0;
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[0].length; j++){
if (matrix[i][j] == '0') height[j] = 0;
else height[j]++;
}
globalmax = Math.max(globalmax, maxrow(height));
}
return globalmax;
}
public int maxrow(int[] height){
Stack<Integer> st = new Stack<>();
int localmax = 0;
for (int i = 0; i <= height.length; i++){
int h = (i == height.length)? 0 : height[i];
while (!st.isEmpty() && height[st.peek()] >= h){
int maxheight = height[st.pop()];
int area = st.isEmpty()? i * maxheight : maxheight * (i - st.peek() -1);
localmax = Math.max(localmax, area);
}
st.push(i);
}
return localmax;
}
}
Java实现 LeetCode 85 最大矩形的更多相关文章
- leetcode 85. 最大矩形
题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 思路分析: 这题是之前那道最大正方形的进阶,同样是利用dp来求解.通过逐行去计算最大矩形,即优化的 ...
- Java实现 LeetCode 492 构造矩形
492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设 ...
- Java实现 LeetCode 391 完美矩形
391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- FPGA学习心得汇总(手中写代码,心中有电路)
http://bbs.ednchina.com/BLOG_ARTICLE_2111172.HTM 任何的时序逻辑都可以转换为组合逻辑+D触发器来完成. FPGA内部主要三块:可编程的逻辑单元.可编程的 ...
- C# 数据操作系列 - 6 EF Core 配置映射关系
0. 前言 在<C# 数据操作系列 - 5. EF Core 入门>篇中,我们简单的通过两个类演示了一下EF增删改查等功能.细心的小伙伴可能看了生成的DDL SQL 语句,在里面发现了些端 ...
- 反射,获取类的属性以及get方法
vo实体类: public class Result { /*** * 返回结果code * ok:10000 * error:20000 */ private String code; /*** * ...
- 12个让您震撼的Linux终端命令
以下快捷键很有用,可以节省你的时间: CTRL+U: 从光标处删除文本直到行首. CTRL+K: 从光标处删除文本直到行尾. CTRL+Y: 粘贴文本. CTRL+E: 将光标移动到行尾. CTRL+ ...
- c# 贪吃蛇小游戏
------------恢复内容开始------------ 新手学习c# 在博客园上看到后自己模仿打出来的第一个程序 开心,纪念一下 bean :食物类 block :蛇块类 snake :蛇类 ...
- percona 5.6的安装
yum 安装, 1 如果已经安装过mysql 的东西,先卸载了.yum remove mysql* 包括 /etc/my.cnf 这个东西卸载的时候不会删除. mv /etc/my.cnf /etc/ ...
- python之PyCharm下载和安装教程
PyCharm 是 JetBrains 公司(www.jetbrains.com)研发,用于开发 Python 的 IDE 开发工具.图 1 所示为 JetBrains 公司开发的多款开发工具,其中很 ...
- Centos7安装jupyter notebook
安装python3 查看当前python版本 [root@iz1i4qd6oynml0z /]# python -V Python 2.7.5 安装python3以及检查python3的版本 yum ...
- JS的函数和对象一
1.递归 在函数的内部调用自身,默认是一个无限循环. 2.匿名函数 没有名称的函数 function(){ } (1)创建函数 函数声明 function fn1(){ } 函数表达式 va ...
- Django之ORM多表关系创建
ORM模型多表逻辑创建: 以图书和作者关系模型为例: models.py from django.db import models ''' 一本书只能被一个出版社出版; 一个出版社可以出版多本书; 一 ...