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 最大矩形的更多相关文章

  1. leetcode 85. 最大矩形

    题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 思路分析: 这题是之前那道最大正方形的进阶,同样是利用dp来求解.通过逐行去计算最大矩形,即优化的 ...

  2. Java实现 LeetCode 492 构造矩形

    492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设 ...

  3. Java实现 LeetCode 391 完美矩形

    391. 完美矩形 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 ...

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

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

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

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

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

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

随机推荐

  1. 关于51单片机IO引脚的驱动能力与上拉电阻设计方案

    转载自:http://bbs.dianyuan.com/article/20312-2 单片机的引脚,可以用程序来控制,输出高.低电平,这些可算是单片机的输出电压.但是,程序控制不了单片机的输出电流. ...

  2. linux输入输出、重定向、管道

    本篇讲述linux系统的输入输出.管道和重定向. 1. liunx的输入输出 一个linux系统要想发挥作用,就要有输入输出,这样才可以与外界交互. 类型 设备文件名 文件描述符 设备名称 说明 备注 ...

  3. (电脑连不上热点)电脑连上了WIFI,但是显示网络不可用怎么办?

    假如WIFI没有问题的话,那这个就是电脑网络堵塞的问题了,下面是解决的办法: 情况一 1.首先win键+R打开运行框,输入cmd 2.然后在命令行输入 ipconfig -release ipconf ...

  4. 编译nginx的时候报错 需要安装PCRE

    ./configure --prefix=/mynginx/ 本地编译nginx的时候 报错 提示需要安装PCRE 错误信息: ./configure: error: the HTTP rewrite ...

  5. Docker容器映射到宿主机只有tcp6没有tcp问题

    问题描述: Docker容器映射到宿主机后,查询端口连接只有tcp6没有tcp,通过ipv4地址连接时无法连接成功. 处理方法: 1.检查是否开启ipv4端口转发 sysctl net.ipv4.ip ...

  6. zsy后台管理系统-界面

    自研平台:(java+vue+mysql+docker) 欢迎大家预览,指导! http://www.zsyai.top/dist 流程引擎 自定义定时任务: 一键生成前后端代码:

  7. Java 集合排序策略接口 Comparator

    1. 前言 最近用到了集合排序(基于 Java 8).现在我能用 Stream 的就用 Stream ,真香!排序可以这么写: List<People> peoples = new Arr ...

  8. eslint插件开发教程

    开发eslint插件目的:根据项目需要,自定义满足项目特殊需要的校验规则 参考eslint官方文档展开阐述 插件开发 自定义规则 单元测试 下面开始通过一个示例demo来介绍插件整个开发流程 代码中出 ...

  9. linux_centos7_时间更新

    EDT:美国时间            CST:中国北京时间 方法一.使用ntpdate从时间服务器更新时间: 1.下载ntpdate组件 yum install -y ntp 2.完成后直接测试 [ ...

  10. 201771010128王玉兰《面向对象程序设计(Java)》第十周学习总结

    第一部分:理论知识部分总结: (1) 定义简单泛型类: A:泛型:也称参数化类型(parameterizedtype),就是在定义类.接口和方法时,通过类型参数指 示将要处理的对象类型. B:泛型程序 ...