LeetCode:Maximal Rectangle
2016-06-16 13:10
267 查看
Maximal Rectangle
Total Accepted: 43628 TotalSubmissions: 183613 Difficulty: Hard
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
Subscribe to see which companies asked this question
Hide Tags
Array Hash
Table Stack Dynamic
Programming
Hide Similar Problems
(H) Largest Rectangle in Histogram (M)
Maximal Square
思路:
解法类似于【Largest Rectangle in Histogram】。
java code:
public class Solution { public int maximalRectangle(char[][] matrix) { if(matrix==null || matrix.length==0) return 0; int m = matrix.length; int n = matrix[0].length; int[] height = new int[n+1]; int maxArea = 0; for(int row = 0;row < m;row++) { Stack<Integer> stack = new Stack<>(); for(int col = 0; col <= n; col++) { if(col < n) { if(matrix[row][col] == '1') height[col]++; else height[col] = 0; } if(stack.isEmpty() || height[col] >= height[stack.peek()]) stack.push(col); else { while(!stack.isEmpty() && height[col] < height[stack.peek()]) { int tmp = stack.pop(); int wid = stack.isEmpty() ? col : col - stack.peek() - 1; maxArea = Math.max(maxArea, height[tmp] * wid); } stack.push(col); } } } return maxArea; } }
相关文章推荐
- Awesomplete 屌爆了
- 文件操作
- Android 踩坑记(一) title 渐变效果
- 前端新手分析 AJAX执行顺序,数据走向
- Android简单的ViewPager指示器
- tomcat内存溢出之PermGen space
- 【leetcode】155. Min Stack【E】【98】
- 滚动条样式设置
- Poj 3259 Wormholes【SPFA判断负权回路】
- php环境设置(win7)
- qqlite 插件开发总结
- sourceTree创建分支和合并代码
- 安装zabbix2.4.8遇到的一些错误
- windows下 composer常见问题及处理
- SIP消息路由
- python名词解释(生成器,匿名函数)
- 保留两位小数,不四舍五入
- Android RecyclerView多个Item布局的实现(可实现头部底部)
- Docker实例教程[超详细](一)
- Python datetime