LeetCode -- Maximal Square
来源:互联网 发布:一个ppp帧的数据部分 编辑:程序博客网 时间:2024/04/29 18:44
题目描述:
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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
在一个矩阵中,找到最大的正方形面积(1表示正方形的点,0表示空)。
本题是很典型的DP问题。
1. 把dp[0,i]赋值为matrix[0,i] , dp[i,0]赋值为matrix[i,0]。i∈[0,n)
2. 两层循环根据不同情况为dp[i,j]赋值:
a. matrix[i,j] == 1 且3个邻居(dp[i-1,j],dp[i,j-1],dp[i-1,j-1])均为1 : dp[i,j] = 4
b. matrix[i,j] == 1 且3个邻居>1且相等: dp[i,j] = (邻居面积的平方根+1)的平方
c. matrix[i,j] == 1 且3个邻居>=1但不一定相等: dp[i,j]=(邻居中最小值的平方根+1)的平方
d. 其他情况: dp[i,j] = matrix[i,j]
3.使用max变量来track当前dp[i,j]的最值
实现代码:
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 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
在一个矩阵中,找到最大的正方形面积(1表示正方形的点,0表示空)。
本题是很典型的DP问题。
1. 把dp[0,i]赋值为matrix[0,i] , dp[i,0]赋值为matrix[i,0]。i∈[0,n)
2. 两层循环根据不同情况为dp[i,j]赋值:
a. matrix[i,j] == 1 且3个邻居(dp[i-1,j],dp[i,j-1],dp[i-1,j-1])均为1 : dp[i,j] = 4
b. matrix[i,j] == 1 且3个邻居>1且相等: dp[i,j] = (邻居面积的平方根+1)的平方
c. matrix[i,j] == 1 且3个邻居>=1但不一定相等: dp[i,j]=(邻居中最小值的平方根+1)的平方
d. 其他情况: dp[i,j] = matrix[i,j]
3.使用max变量来track当前dp[i,j]的最值
实现代码:
public class Solution { public int MaximalSquare(char[,] matrix) { var row = matrix.GetLength(0); var col = matrix.GetLength(1); if(row < 2){ if(row == 0){return 0;}else if(col == 1){return matrix[0,0] == '1' ? 1 : 0;} } var max = 0; var dp = new int[row, col]; for(var i = 0;i < row; i++){ var x = matrix[i,0] == '1' ? 1 : 0;dp[i, 0] = x;if(dp[i,0] > max){max = dp[i,0];} } for(var i = 0;i < col; i++){ var x = matrix[0,i] == '1' ? 1 : 0;dp[0, i] = x;if(dp[0,i] > max){max = dp[0,i];} } for(var i = 1;i < row; i++){ for(var j = 1;j < col; j++){// neighbours all equals 1 if(matrix[i,j] == '1' && dp[i-1,j] == 1 && dp[i, j-1] == 1 && dp[i-1,j-1] == 1){if(dp[i-1, j] == 1){dp[i,j] = 4;} }// neighbours all bigger than 1 and equals each otherelse if(matrix[i,j] == '1' && dp[i-1,j] == dp[i,j-1] && dp[i-1,j-1] == dp[i-1,j] && dp[i-1,j] > 1){dp[i,j] = (int)Math.Pow(Math.Sqrt(dp[i,j-1]) + 1,2);}// neighbours all no less than 1, but may not equals each otherelse if(matrix[i,j] == '1' && dp[i-1,j] >= 1 && dp[i,j-1] >= 1 && dp[i-1,j-1] >= 1){var min = Math.Min(Math.Min(dp[i-1,j-1], dp[i-1,j]), dp[i,j-1]);dp[i,j] = (int)Math.Pow(Math.Sqrt(min) + 1,2);} else{ dp[i,j] = matrix[i,j] == '1' ? 1 : 0; }if(dp[i,j] > max){max = dp[i,j];} } } return max; }}
0 0
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- [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
- leetcode Maximal Square
- Leetcode 221 Maximal Square
- leetCode(27):Maximal Square
- [leetCode] Maximal Square
- Leetcode 221 Maximal Square
- leetcode题目 寻找和为SUM的两数(O(nlogn)和O(n)解法)
- codeforces 22B B. Bargaining Table(dp)
- jQuery UI基础----3jQuery UI Interactions-droppable(适于投下的
- LeetCode -- Max Points on a Line
- UVA_815: Flooded!
- LeetCode -- Maximal Square
- jQuery UI基础----4jQuery UI Interactions-resizeable( 可变尺寸的
- Reverse bits of a given 32 bits unsigned integer
- Codeforces Round #323 (Div. 2) (583A,583B,582A,582B)
- LeetCode -- Summary Ranges
- HDU 3548 Enumerate the Triangles(找周长最小的三角形+优化)
- jQuery UI基础----5jQuery UI Interactions-selectable(可选择的
- win7里设置VC6中的字体
- 通过身边小事解释机器学习是什么?