在一个只包含0,1的二维数组中找到最大矩形的面积
2016-03-29 22:46
459 查看
1、用一个height数组表示从从上到下连续的1的高度。left,right表示当前矩形的边界。maxA = max(maxA,(right[j]-left[j])*height[j]);
例如:
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
对第一行:
l: 0 0 0 3 0 0 0
r: 7 7 7 4 7 7 7
对第二行:
l: 0 0 2 3 2 0 0
r: 7 7 5 4 5 7 7
对第三行:
l: 0 1 2 3 2 1 0
r: 7 6 5 4 5 6 7
2、先计算出高度,在用stack来计算最大的面积。O(n^2)
3、计算出高度后直接暴力计算。O(n^3)
例如:
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
对第一行:
l: 0 0 0 3 0 0 0
r: 7 7 7 4 7 7 7
对第二行:
l: 0 0 2 3 2 0 0
r: 7 7 5 4 5 7 7
对第三行:
l: 0 1 2 3 2 1 0
r: 7 6 5 4 5 6 7
int maximalRectangle(int[][] matrix) { if (matrix.length == 0)return 0; int m = matrix.length; int n = matrix[0].length; int left[] = new int ; int right[] = new int ; int height[] = new int ; Arrays.fill(left, 0); Arrays.fill(right, n); Arrays.fill(height, 0); int maxA = 0; for (int i = 0; i < m; i++) { int cur_left = 0, cur_right = n; for (int j = 0; j < n; j++) { // compute height (can do this from either side) if (matrix[i][j] == '1') height[j]++; else height[j] = 0; } for (int j = 0; j < n; j++) { // compute left (from left to right) if (matrix[i][j] == '1') left[j] = Math.max(left[j], cur_left); else { left[j] = 0; cur_left = j + 1; } } // compute right (from right to left) for (int j = n - 1; j >= 0; j--) { if (matrix[i][j] == '1') right[j] = Math.min(right[j], cur_right); else { right[j] = n; cur_right = j; } } // compute the area of rectangle (can do this from either side) for (int j = 0; j < n; j++) maxA = Math.max(maxA, (right[j] - left[j]) * height[j]); } return maxA; }
2、先计算出高度,在用stack来计算最大的面积。O(n^2)
int maximalRectangle(int[][] matrix) { if (matrix.length == 0)return 0; int m = matrix.length; int n = matrix[0].length; int height[] = new int[n+1]; height =0; int maxA = 0; for (int i = 0; i < m; i++) { //compute height for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') height[j]++; else height[j] = 0; } //compute max area by height with stack Stack<Integer> s = new Stack<>(); for (int j = 0; j <= n; j++) { if (s.isEmpty()||height[s.peek()]<=height[j]) s.push(j); else { while(!s.isEmpty()&&height[j]<height[s.peek()]){ maxA =Math.max(maxA, height[s.pop()]*(s.isEmpty()?j:(j-s.peek()-1))) ; } s.push(j); } } } return maxA; }
3、计算出高度后直接暴力计算。O(n^3)
int maximalRectangle(int[][] matrix) { if (matrix.length == 0)return 0; int m = matrix.length; int n = matrix[0].length; int height[] = new int[n+1]; height =0; int maxA = 0; for (int i = 0; i < m; i++) { //compute height for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') height[j]++; else height[j] = 0; } //compute max area by height maxA=Math.max(maxA,getMaxArea(height)); } return maxA; } public int getMaxArea(int []height){ int maxA=0; for(int i=0;i<height.length;i++){ int j=i-1; //compute maxHeight in i...0 end with i int maxtemp=height[i]; int minHeight=height[i]; while(j>=0){ if(height[j]==0)break; minHeight=Math.min(minHeight, height[j]); maxtemp=Math.max(maxtemp, minHeight*(i-j+1)); j--; } maxA=Math.max(maxtemp, maxA); } return maxA; }
相关文章推荐
- JVM-class文件完全解析-常量池
- Wireshark-ICMP数据报分析
- js实现Enter键提交表单(兼容各个浏览器)
- Deploy Django app on amazon ec2
- 夜晚 十点 React-Native 源码 暴力畜 系列
- Shell与其命令语法分析
- 关于使用OpenProcess的一个错误
- hdu1421(dp)
- 滑动嵌套
- 电脑环境变量里面的参数
- Supervisor Unix系统进程管理工具
- 《leetCode》: Maximal Rectangle
- JAVA内存管理与优化
- ERROR 1366 (HY000): Incorrect string value: '\xE7\x9A\x84' for column 'name' at row 1
- Leetcode - Median of Two Sorted Arrays
- 做程序员的苦与乐,只有你能懂!
- C++ Primer读书笔记-ch01
- Data URI Scheme -- data:image/jpg;base64是什么
- Markdown 语法的简要规则
- 另种获取key和value