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. 如何将Altera官方提供的CADENCE.OLB应用于altium Designer中

  2. chrome安装工具

    0x00 简介 今天在知识星球的小迪渗透吧对外交流群里看到Web安全从业者必备Chrome插件这篇帖子,看完之后,我虽然还是个学生,但我也是个垃圾啊.我的chrome上面没有一个上面描述的工具,真的是 ...

  3. 当Tomcat遇上Netty

    故事背景 嘀嘀嘀~,生产事故,内存泄漏! 昨天下午,突然收到运维的消息,分部某系统生产环境内存泄漏了,帮忙排查一下. 排查过程 第一步,要日志 分部给到的异常日志大概是这样(鉴于公司规定禁止截图禁止拍 ...

  4. pssh远程执行命令的利器

    pssh -h hosts.txt -l irb2 -o /tmp/foo uptime -l 后面加用户,很好理解,执行uptime,然后把结果写入/tmp/foo目录. pscp -h hosts ...

  5. MySQLdb安装记

    1 安装 python-devel 2. site.cfg 改mysql_config成实际位置 mysql_config = /mysqldata/mariadb530/bin/mysql_conf ...

  6. 敏捷为什么会失败之「PA-SA-WAKA-DA」理论

    在日常生活中,有种有趣的现象:我们更津津乐道于美好的故事,比如提到好莱坞,我们关注的只是大牌明星,却忽略了他们成名其背后的艰辛.对于那些成功的敏捷项目,也是如此.在我们见证成功的同时,却忘记了项目团队 ...

  7. Codeforces1183A(A题)Nearest Interesting Number

    Polycarp knows that if the sum of the digits of a number is divisible by 3, then the number itself i ...

  8. Django模板之模板标签

    标签比变量更加复杂:一些在输出中创建文本,一些通过循环或逻辑来控制流程,一些加载其后的变量将使用到的额外信息到模版中. 一些标签需要开始和结束标签 (例如:{% tag %} ...标签 内容 ... ...

  9. BZOJ 1028 BZOJ 1029 //贪心

    1028: [JSOI2007]麻将 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2197  Solved: 989[Submit][Status][ ...

  10. 【Mac 实用技巧】不定期更新

    Mac去掉截屏图片边框外阴影效果 一次命令行:defaults write com.apple.screencapture disable-shadow -bool true;\killall Sys ...