LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
2017-11-06 15:37
495 查看
题目链接
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:
Return 4.
解:dp[i][j]表示以(i, j)为右下顶点的正方形的最长边长,那么我们所求的最大的面积则可以表示为max{ dp[i][j] }的平方,其中0<=i<height, 0<=j<width,易得转移方程:dp[i][j] = min{ dp[i-1][j], dp[i][j-1],
dp[i-1][j-1] }+1,可以在计算dp数组的同时维护一个maxLen(最长边长)变量,计算完成后得到的maxLen最终值平方就是答案,算法的复杂度为O(height*width),代码:
题目链接
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:
Return 6.
解:和上题很相似,但是条件是矩形后情况就会比较复杂,对于(i, j),我们需要分别考虑他的最大width(以(i,
j)为右顶点最长‘1‘线段长)和最大height(竖直的以(i, j)结尾的最长‘1’线段长),例如example中dpheight[2][3] = 2, dpwidth[2][3] = 4,而且最大矩形面积也不能简单的由最大长宽相乘获得,分别求得了dpheight和dpwidth后,可以以dpheight[i][j]向上遍历求得以(i, j)为右下顶点的每个矩形面积,也可以以dpwidth[i][j]向左遍历求得矩形面积,最终结果就是所有矩形面积的最大值了。
为了提高算法的效率,我们可以先只计算dpwidth,转移方程:dpwidth[i][j] = matrix[i][j] == '0' ? 0 : dpwidth[i][j-1]+1,然后对于每个(i, j),动态地计算所有矩形面积,算法的时间复杂度O(width*height*height),代码:
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.
解:dp[i][j]表示以(i, j)为右下顶点的正方形的最长边长,那么我们所求的最大的面积则可以表示为max{ dp[i][j] }的平方,其中0<=i<height, 0<=j<width,易得转移方程:dp[i][j] = min{ dp[i-1][j], dp[i][j-1],
dp[i-1][j-1] }+1,可以在计算dp数组的同时维护一个maxLen(最长边长)变量,计算完成后得到的maxLen最终值平方就是答案,算法的复杂度为O(height*width),代码:
class Solution { public: int maximalSquare(vector<vector<char> >& matrix) { int height = matrix.size(); if (height == 0) return 0; int width = matrix[0].size(); int** dp = new int*[height]; int maxLen = 0; for (int i = 0; i < height; i++) dp[i] = new int[width]; for (int i = 0; i < height; i++) { dp[i][0] = matrix[i][0]-'0'; maxLen = max(maxLen, dp[i][0]); } for (int i = 0; i < width; i++) { dp[0][i] = matrix[0][i]-'0'; maxLen = max(maxLen, dp[0][i]); } for (int i = 1; i < height; i++) { for (int j = 1; j < width; j++) { if (matrix[i][j] == '0') dp[i][j] = 0; else dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1])+1; maxLen = max(maxLen, dp[i][j]); } } return maxLen*maxLen; } };
题目链接
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.
解:和上题很相似,但是条件是矩形后情况就会比较复杂,对于(i, j),我们需要分别考虑他的最大width(以(i,
j)为右顶点最长‘1‘线段长)和最大height(竖直的以(i, j)结尾的最长‘1’线段长),例如example中dpheight[2][3] = 2, dpwidth[2][3] = 4,而且最大矩形面积也不能简单的由最大长宽相乘获得,分别求得了dpheight和dpwidth后,可以以dpheight[i][j]向上遍历求得以(i, j)为右下顶点的每个矩形面积,也可以以dpwidth[i][j]向左遍历求得矩形面积,最终结果就是所有矩形面积的最大值了。
为了提高算法的效率,我们可以先只计算dpwidth,转移方程:dpwidth[i][j] = matrix[i][j] == '0' ? 0 : dpwidth[i][j-1]+1,然后对于每个(i, j),动态地计算所有矩形面积,算法的时间复杂度O(width*height*height),代码:
class Solution { public: int maximalRectangle(vector<vector<char> >& matrix) { int height = matrix.size(); if (height == 0) return 0; int width = matrix[0].size(); int** dpwidth = new int*[height]; for (int i = 0; i < height; i++) dpwidth[i] = new int[width]; int maxArea = 0; for (int i = 0; i < height; i++) { dpwidth[i][0] = matrix[i][0]-'0'; for (int t = i; t >= 0 && matrix[t][0] == '1'; t--) { maxArea = max(maxArea, i-t+1); } } for (int i = 0; i < height; i++) { for (int j = 1; j < width; j++) { dpwidth[i][j] = matrix[i][j] == '0' ? 0 : dpwidth[i][j-1]+1; int wid = width; for (int t = i; t >= 0 && matrix[t][j] == '1'; t--) { wid = min(wid, dpwidth[t][j]); maxAre 4000 a = max(maxArea, wid*(i-t+1)); } } } return maxArea; } };
相关文章推荐
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- leetcode085_221:Maximal Rectangle&Maximal Square
- 【LeetCode】Maximal Rectangle && Maximal Square
- 221. Maximal Square && 85. Maximal Rectangle && 84. Largest Rectangle in Histogram
- leetcode 221. Maximal Square 最大正方形面积 + 动态规划DP实现
- [LeetCode]221. Maximal Square & 85. Maximal Rectangle
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
- 每周LeetCode算法题(八): 题目: 221. Maximal Square & 85. Maximal Rectangle
- LeetCode221. Maximal Square
- [leetcode] 221 Maximal Square(最大全1正方形 &动态规划)
- leetcode 221. Maximal Square
- [leetCode]53. Maximum Subarray&&动态规划
- LeetCode 85. Maximal Rectangle(java)
- CF 135B Rectangle and Square(正方形判断 &amp; 矩形判断)(数学)
- [LeetCode] 221. Maximal Square 最大正方形
- 【Leetcode】Rectangle Area && Classic Prob: Overlap Rectangle
- 【LeetCode】221. Maximal Square
- **Leetcode 221. Maximal Square | dp
- leetcode 221. Maximal Square
- LeetCode 85. Maximal Rectangle