【LeetCode】Maximal Rectangle && Maximal Square
来源:互联网 发布:凯旋软件 编辑:程序博客网 时间:2024/05/01 03:27
1、Maximal Rectangle
Total Accepted: 24875 Total Submissions: 113379 My Submissions Question Solution
Given a 2D binary matrix filled with 0's and 1's,
find the largest rectangle containing all ones and return its area.
2、Maximal Square
Total Accepted: 1488 Total Submissions: 7283 My Submissions Question Solution
Given a 2D binary matrix filled with 0's and 1's,
find the largest square containing all 1's and return its area.
For example, given the following matrix:
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
【解题思路】
参考Largest Rectangle in Histogram,解题思路参考【LeetCode】Largest Rectangle in Histogram,
针对二维矩阵的每行,其实就是一个单独的Largest Rectangle。
针对长方形,思路和Largest Rectangle一致。
针对正方形,每次计算square的时候,取长和宽最小值的平方即为面积。
Total Accepted: 24875 Total Submissions: 113379 My Submissions Question Solution
Given a 2D binary matrix filled with 0's and 1's,
find the largest rectangle containing all ones and return its area.
2、Maximal Square
Total Accepted: 1488 Total Submissions: 7283 My Submissions Question Solution
Given a 2D binary matrix filled with 0's and 1's,
find the largest square containing all 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 0Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
【解题思路】
参考Largest Rectangle in Histogram,解题思路参考【LeetCode】Largest Rectangle in Histogram,
针对二维矩阵的每行,其实就是一个单独的Largest Rectangle。
针对长方形,思路和Largest Rectangle一致。
针对正方形,每次计算square的时候,取长和宽最小值的平方即为面积。
1 Java AC
public class Solution { public int maximalRectangle(char[][] matrix) { if(matrix == null || matrix.length == 0){ return 0; } int m = matrix.length; int n = matrix[0].length; int height[][] = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(matrix[i][j] == '0'){ height[i][j] = 0; }else{ height[i][j] = i == 0 ? 1 : height[i-1][j] + 1; } } } int maxArea = 0; for(int i = 0; i < m; i++){ int area = largestRectangleArea(height[i]); maxArea = area > maxArea ? area : maxArea; } return maxArea; } public int largestRectangleArea(int[] height) { if(height == null || height.length == 0){ return 0; } int len = height.length; Stack<Integer> stack = new Stack<Integer>(); int area = 0; for(int i = 0; i <= len; i++){ int h = i == len ? 0 : height[i]; if(stack.isEmpty() || height[stack.peek()] <= h){ stack.push(i); continue; } int start = stack.pop(); int width = stack.empty() ? i : i - stack.peek() - 1; area = Math.max(area, height[start] * width); i--; } return area; }}2 Java AC
public class Solution { public int maximalSquare(char[][] matrix) { if(matrix == null || matrix.length == 0){ return 0; } int m = matrix.length; int n = matrix[0].length; int height[][] = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(matrix[i][j] == '0'){ height[i][j] = 0; }else{ height[i][j] = i == 0 ? 1 : height[i-1][j] + 1; } } } int maxArea = 0; for(int i = 0; i < m; i++){ int area = largestRectangleArea(height[i]); maxArea = area > maxArea ? area : maxArea; } return maxArea; } public int largestRectangleArea(int[] height) { if(height == null || height.length == 0){ return 0; } int len = height.length; Stack<Integer> stack = new Stack<Integer>(); int area = 0; for(int i = 0; i <= len; i++){ int h = i == len ? 0 : height[i]; if(stack.isEmpty() || height[stack.peek()] <= h){ stack.push(i); continue; } int start = stack.pop(); int width = stack.empty() ? i : i - stack.peek() - 1; int min = Math.min(height[start], width); area = Math.max(area, min*min); i--; } return area; }}
0 0
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- [LeetCode]221. Maximal Square & 85. Maximal Rectangle
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
- leetcode085_221:Maximal Rectangle&Maximal Square
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- [leetcode] Maximal Square
- Maximal Square Leetcode Java
- [leetcode] Maximal Square
- leetcode 221: Maximal Square
- LeetCode Maximal Square
- Maximal Square - LeetCode 221
- #leetcode#Maximal Square
- LeetCode Maximal Square
- [LeetCode] Maximal Square
- [LeetCode]Maximal Square
- leetcode Maximal Square
- IOS开发笔记(2)页面之间传递数据几种方式比较
- MSSQL备份语句
- Hadoop2.61集群安装配置教程
- Android开源项目分类汇总
- 查看死锁
- 【LeetCode】Maximal Rectangle && Maximal Square
- ant打包遇到了系统资源不足
- 生成学习算法概述
- 微信JS-SDK如何使用
- 查看SQL资源使用情况
- Annotations
- ListView fast scroll style
- linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)
- Android开发的四大组件