每周LeetCode算法题(八): 题目: 221. Maximal Square & 85. Maximal Rectangle
来源:互联网 发布:淘宝双11报名 编辑:程序博客网 时间:2024/05/16 10:55
每周LeetCode算法题(八)
题目1: 221. Maximal Square
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0Return 4.
解法分析
题目的要求是,找出一个0/1矩阵中,由1构成的最大方阵的面积。
这是一道动态规划的题目,关键是要能得出递推式是dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1。同时还能发现dp[i]只和dp[i]以及dp[i - 1]有关系,因此还能压缩需要的空间,这里只用两个长度m的数组就能替代n*m大小的数组。
C++代码
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int n = matrix.size(); if (n == 0) { return 0; } int m = matrix[0].size(); int* dp1 = new int[m]; memset(dp1, 0, sizeof(int) * m); int* dp2 = new int[m]; memset(dp2, 0, sizeof(int) * m); int ans = 0; for (int i = 0; i < m; i++) { if (matrix[0][i] == '1') { dp1[i] = 1; ans = 1; } } for (int i = 1; i < n; i++) { dp2[0] = matrix[i][0] - '0'; ans = max(ans, dp2[0]); for (int j = 1; j < m; j++) { if (matrix[i][j] == '1') { dp2[j] = min(dp1[j - 1], min(dp2[j - 1], dp1[j])) + 1; ans = max(ans, dp2[j]); } } int * tmp = dp1; dp1 = dp2; dp2 = tmp; memset(dp2, 0, sizeof(int) * m); } return ans * ans; }};
题目2: 85. 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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0Return 6.
解法分析
题目看起来很像上一题,只不过目标从方阵放松到矩阵。但这样就难上了许多。
解题思路和上一题,除了同时动态规划以外,联系倒不大。
先定义height, left, right三个长度为矩阵宽度m的数组,其中height[j]用于表示由1构成的矩阵的高,right[j] - left[j]用于表示它的宽(0<=j< m).
对给出的矩阵逐行扫描,这样可以先求得所有height[j]为每一列上1连续出现的数量,即1构成的矩阵的高。若1断连,则该列的高重置为0.
逐行扫描过程中,高既已求得,接下来就要求宽了。分别从左到右和从右到左找出对应每一列,在高为height[j]的情况下能满足矩阵条件的left[j]值和right[j]值,即在高不减小的情况下,宽度不能增大。
说得不够清晰,还是看代码吧。
C++代码
class Solution {public: int maximalRectangle(vector<vector<char>>& matrix) { int n = matrix.size(); if (n == 0) { return 0; } int m = matrix[0].size(); int * height = new int[m]; int * left = new int[m]; int * right = new int[m]; memset(height, 0, sizeof(int) * m); memset(left, 0, sizeof(int) * m); fill_n(right, m, m); int ans = 0; for (int i = 0; i < n; i++) { int l = 0; int r = m; for (int j = m - 1; j >= 0; j--) { if (matrix[i][j] == '1') { right[j] = min(right[j], r); } else { right[j] = m; r = j; } } for (int j = 0; j < m; j++) { if (matrix[i][j] == '1') { height[j]++; left[j] = max(left[j], l); } else { height[j] = 0; left[j] = 0; l = j + 1; } ans = max(ans, height[j] * (right[j] - left[j])); } } return ans; }};
- 每周LeetCode算法题(八): 题目: 221. Maximal Square & 85. Maximal Rectangle
- [LeetCode]221. Maximal Square & 85. Maximal Rectangle
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- LeetCode题目85.Maximal Rectangle(Hard)
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
- [leetcode] 221.Maximal Square
- 【leetcode】221. Maximal Square
- [LeetCode]221. Maximal Square
- leetcode 221. Maximal Square
- 221. Maximal Square LeetCode
- LeetCode 221. Maximal Square
- LeetCode *** 221. Maximal Square
- LeetCode-221.Maximal Square
- leetcode.221. Maximal Square
- Leetcode 221. Maximal Square
- mysql基础
- 覃超点评F8大会:还是记忆中的Facebook,黑科技程度没两样 | 硬创公开课预告
- video pixel networks阅读笔记
- Gtktreeview链表和树型存储结构
- 用AXIS2发布WebService的方法 使用eclipse插件生成服务端和客户端
- 每周LeetCode算法题(八): 题目: 221. Maximal Square & 85. Maximal Rectangle
- Python3 GUI中Tkinter
- 【边双联通分量】hihoCoder#1184 连通性二·边的双连通分量
- 07-面向对象(多态-类型判断-instanceof)1 2 3 4 5
- Convolutional neural network transfer for automated glaucoma identification论文理解
- 一只爬虫带你看世界【7】
- MyEclipse忽略单个文件的错误提示操作
- luogu1624 单词缩写
- java中对类中的成员变量和成员方法的封装及private关键字