每周LeetCode算法题(八): 题目: 221. Maximal Square & 85. Maximal Rectangle
2017-10-27 21:14
357 查看
每周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 0
Return 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 0
Return 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算法题(七): 题目: 198. House Robber
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- [LeetCode]221. Maximal Square & 85. Maximal Rectangle
- 【python】【leetcode】【算法题目12—Integer to Roman】
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
- LeetCode算法题目:Number of Digit One
- LeetCode算法题目:Candy
- leetcode 算法复习题目
- [leetcode.com]算法题目 - Decode Ways
- 【LeetCode】Two Sum && 【九度】题目1352:和为S的两个数字
- [leetcode.com]算法题目 - Maximum Subarray
- 【python】【leetcode】【算法题目344—Reverse String】
- [LeetCode]环链表相关的题目和算法
- LeetCode算法题目:Symmetric Tree
- 每周LeetCode算法题(六): 617. Merge Two Binary Trees
- Leetcode 221. Maximal Square
- leetcode 221. Maximal Square
- [leetcode.com]算法题目 - Same Tree
- [leetcode]221. Maximal Square