LeetCode85 Maximal Rectangle
来源:互联网 发布:如何注销知乎账号 编辑:程序博客网 时间:2024/06/05 12:43
LeetCode85 Maximal Rectangle
问题来源 LeetCode85
问题描述
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
For example, given the following matrix:
1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0
Return 6.
问题分析
这道题目其实和LeetCode84有很大的关系。比如题目给定的矩阵。
1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0
可以转化成
1 0 1 0 02 0 2 1 13 1 3 2 24 0 0 3 0
也就是把每列连续的1 加起来,形成新的列,而矩形的面积可以通过Rectangle in Histogram的规则计算出来。
比如第3行的 3 2 2 就是给定矩阵的最大矩形,也就是2 +2 +2 =6。
所以解决这道题的方法就是先把给矩阵的每一行转化,然后利用84题的算法进行计算,并保存最大值。
代码如下
public int maximalRectangle(char[][] matrix) { if(matrix==null|| matrix.length<1 ||matrix[0].length<1) return 0; int m =matrix.length; int n = matrix[0].length; for(int i=1;i<m;i++){ for (int j = 0; j < n; j++) { if(matrix[i][j]!='0'){ matrix[i][j]=(char)(matrix[i][j]+matrix[i-1][j]-'0'); } } } int max =0; for (int i = 0; i < m; i++) { max =Math.max(max,largestRectangleArea(matrix[i])); } return max;}public int largestRectangleArea(char[] heights) { if(heights==null ||heights.length<1){ return 0; } int result = 0; Stack<Integer> stack = new Stack<>(); stack.add(-1); for(int i=0;i<heights.length;i++){ while(stack.size()!=1 && heights[stack.peek()]>=heights[i]){ result = Math.max((heights[stack.pop()]-'0')*(i-stack.peek()-1),result); } stack.add(i); } int right = stack.peek(); while (stack.size()>1){ result = Math.max((heights[stack.pop()]-'0')*(right-(stack.peek())),result); } return result;}
LeetCode学习笔记持续更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678
阅读全文
0 0
- leetcode85 maximal rectangle
- LeetCode85. Maximal Rectangle
- LeetCode85. Maximal Rectangle
- leetcode85 85. Maximal Rectangle
- Leetcode85. Maximal Rectangle.
- LeetCode85 Maximal Rectangle
- LeetCode85 Maximal Rectangle java题解
- LeetCode85——Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Qt5学习-模板库、工具类、控件-1
- POJ 1469COURSES
- VHDL中信号与变量的区别及赋值的讨论
- psql -W
- 第K小数(二分)
- LeetCode85 Maximal Rectangle
- Gourmet and Banquet CodeForces
- 匈牙利板子
- 笔试题:如何判断单链表是否存在环
- 升为主程序,总希望总结点什么
- 动态规划-数组最长递增子序列长度
- 从硬币找零问题:看分治/动态规划/贪心算法的区别与联系
- psql中使用\set为某个命令创建别名
- LPC1788 U盘读写及IAP的实现 第二章