leetcode 最大矩形和
2014-07-12 16:01
381 查看
1.枚举法(超时)
View Code
https://oj.leetcode.com/problems/maximal-rectangle/
class node { int d; //di int h;// higtht } public class Solution { public int largest(int[] height) { int len=height.length; if(len==0) return 0; node n[]=new node[len]; for(int i=0;i<len;i++) { n[i]=new node(); n[i].d=1; n[i].h=height[i]; } Stack<node> s=new Stack<node>(); s.push(n[0]); int max=n[0].h; for(int j=1;j<len;j++) { node t=s.peek(); if(n[j].h>=t.h) { s.push(n[j]); } else { int with=0; while(!s.isEmpty()&&s.peek().h>n[j].h) { t=s.pop(); with+=t.d; if(with*t.h>max) max=with*t.h; } n[j].d+=with; s.push(n[j]); } } int with=0; while(!s.isEmpty()) { node t=s.pop(); with=with+t.d; int temp=t.h*with; if(temp>max) max=temp; } return max; } public int maximalRectangle(char[][] matrix) { if(matrix.length==0) return 0; int len=matrix[0].length; int ans[]=new int[len]; for(int i=0;i<len;i++) { ans[i]=matrix[0][i]-'0'; } int max=largest(ans); for(int i=1;i<matrix.length;i++) { for(int j=0;j<matrix[0].length;j++) { if(matrix[i][j]=='0') ans[j]=0; else { ans[j]+=1; } } int t=largest(ans); if(max<t) max=t; } return max; } }
View Code
https://oj.leetcode.com/problems/maximal-rectangle/
相关文章推荐
- [C++]LeetCode 11: Container With Most Water(最大容积/最大矩形面积)
- [LeetCode] 01矩阵中最大矩形 Maximal Rectangle
- Leetcode刷题记—— 84. Largest Rectangle in Histogram(柱形图中最大矩形面积)
- Leetcode 85. Maximal Rectangle 最大矩形 解题报告
- [LeetCode] Maximal Rectangle 最大矩形
- LeetCode 85 Maximal Rectangle (最大子矩形 单调栈)
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- Leetcode 363. 矩形区域不超过 K 的最大数值和
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
- [Leetcode] maximal rectangle 最大矩形
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- [LeetCode]Maximal Rectangle寻找最大矩形
- 【LeetCode】直方图最大矩形覆盖(栈) - Hard
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- LeetCode 84. Largest Rectangle in Histogram(直方图最大矩形)
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- leetcode之Largest Rectangle in Histogram、Maximal Rectanglex;待字闺中之最大矩形分析
- 每天一道LeetCode-----计算直方图中最大矩形的面积