LeetCode - Maximal Rectangle
2016-01-03 17:21
232 查看
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
思路:
类似于上一篇文章中的方法一,对每列的左右拓展极限进行记录,同时保存当前列的高度。
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
思路:
类似于上一篇文章中的方法一,对每列的左右拓展极限进行记录,同时保存当前列的高度。
package area; public class MaximalRectangle { public int maximalRectangle(char[][] matrix) { int m; int n; if (matrix == null || (m = matrix.length) == 0 || (n = matrix[0].length) == 0) return 0; // Height of this column int[] H = new int ; int[] L = new int ; int[] R = new int ; for (int i = 0; i < n; ++i) { H[i] = 0; L[i] = 0; R[i] = n; } int max = 0; for (int i = 0; i < m; ++i) { int left = 0; int right = n; for (int j = 0; j < n; ++j) { if (matrix[i][j] == '1') { ++H[j]; L[j] = Math.max(L[j], left); } else { left = j + 1; H[j] = 0; L[j] = 0; R[j] = n; } } for (int j = n - 1; j >= 0; --j) { if (matrix[i][j] == '1') { R[j] = Math.min(R[j], right); max = Math.max((R[j] - L[j]) * H[j], max); } else { right = j; } } } return max; } public static void main(String[] args) { // TODO Auto-generated method stub char[][] matrix = { { '0', '1', '1', '0', '1' }, { '1', '1', '0', '1', '0' }, { '0', '1', '1', '1', '0' }, { '1', '1', '1', '1', '0' }, { '1', '1', '1', '1', '1' }, { '0', '0', '0', '0', '0' } }; MaximalRectangle m = new MaximalRectangle(); System.out.println(m.maximalRectangle(matrix)); } }
相关文章推荐
- leetcode第9题——*Palindrome Number
- George and Sleep
- view的封装,xib和stroryBoard加载及使用
- 学过JDBC才知道前一篇文章的经历完全可以避免。。。
- CentOS7中自编的一个支持suid的system函数(c语言)
- Python—装饰器
- String类之indexOf--->查找某字对应的位置
- FPGA笔记 AS和JTAG下载方式区别
- java学习细节知识点总结
- java中开发工具版本不对造成的后果之一
- [Leetcode]3. Longest Substring Without Repeating Characters
- 2015前端生态发展回顾
- 谷歌网站
- Apache实现禁止中文浏览器访问与301重定向的方法
- [leetcode] 11. Container With Most Water 解题报告
- UI组件的呈现
- 笔试题中 3种错误处理的方法
- QUESTION 47 How many copies of the alert log are stored in the directory specified by the initializa
- uva 1557 - Calendar Game(博弈)
- 瞠目结舌的JavaScript技巧