leetcode
来源:互联网 发布:如何申请淘宝小号涮单 编辑:程序博客网 时间:2024/06/06 06:58
Maximal Rectangle
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.
Solution:
public int maximalRectangle(char[][] matrix) { if (matrix == null || matrix.length == 0) { return 0; } int m = matrix.length; int n = matrix[0].length; int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(right, n); int[] height = new int[n]; int maxArea = 0; for (int i = 0; i < m; i++) { int cur_left = 0; int cur_right = n; for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') left[j] = Math.max(left[j], cur_left); else { left[j] = 0; cur_left = j + 1; } } for (int j = n - 1; j > -1; j--) { if (matrix[i][j] == '1') right[j] = Math.min(right[j], cur_right); else { right[j] = n; cur_right = j; } } for (int j = 0; j < n; j++) { if (matrix[i][j] == '1') height[j]++; else height[j] = 0; } for (int j = 0; j < n; j++) { maxArea = Math.max(maxArea, height[j] * (right[j] - left[j])); } } return maxArea; }
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- poj 3185 The Water Bowls 反转问题
- tes
- JS第一天
- 艳辉活动——迎春3月带你飞
- ORACLE 导入总结
- leetcode
- 开源免费的.NET图像即时处理的组件ImageProcessor
- 构造函数和析构函数小结
- iOS web view 加载本地HTML页面不显示问题
- leetcode
- 用Visual Studio Code Debug世界上最好的语言
- 算法练习之 (汉诺塔)
- request与response的用法
- C语言中常用的文件操作函数