85:最大矩形
This commit is contained in:
parent
42074b5a5d
commit
44bfc0817b
103
src/main/java/leetcode/editor/cn/MaximalRectangle.java
Normal file
103
src/main/java/leetcode/editor/cn/MaximalRectangle.java
Normal file
@ -0,0 +1,103 @@
|
||||
//给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"]
|
||||
//,["1","0","0","1","0"]]
|
||||
//输出:6
|
||||
//解释:最大矩形如上图所示。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:matrix = []
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [["0"]]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [["1"]]
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [["0","0"]]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// rows == matrix.length
|
||||
// cols == matrix[0].length
|
||||
// 0 <= row, cols <= 200
|
||||
// matrix[i][j] 为 '0' 或 '1'
|
||||
//
|
||||
// Related Topics 栈 数组 哈希表 动态规划
|
||||
// 👍 935 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//85:最大矩形
|
||||
public class MaximalRectangle {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MaximalRectangle().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maximalRectangle(char[][] matrix) {
|
||||
int m = matrix.length;
|
||||
if (m == 0) {
|
||||
return 0;
|
||||
}
|
||||
int n = matrix[0].length;
|
||||
int[][] left = new int[m][n];
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (matrix[i][j] == '1') {
|
||||
left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (matrix[i][j] == '0') {
|
||||
continue;
|
||||
}
|
||||
int width = left[i][j];
|
||||
int area = left[i][j];
|
||||
for (int k = i - 1; k >= 0; k--) {
|
||||
width = Math.min(width, left[k][j]);
|
||||
area = Math.max(area, (i - k + 1) * width);
|
||||
}
|
||||
ret = Math.max(ret, area);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
51
src/main/java/leetcode/editor/cn/MaximalRectangle.md
Normal file
51
src/main/java/leetcode/editor/cn/MaximalRectangle.md
Normal file
@ -0,0 +1,51 @@
|
||||
<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg" style="width: 402px; height: 322px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>最大矩形如上图所示。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = []
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [["0"]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [["1"]]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [["0","0"]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>rows == matrix.length</code></li>
|
||||
<li><code>cols == matrix[0].length</code></li>
|
||||
<li><code>0 <= row, cols <= 200</code></li>
|
||||
<li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>数组</li><li>哈希表</li><li>动态规划</li></div></div>\n<div><li>👍 935</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user