-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathNo221.maximal-square.cs
43 lines (39 loc) · 1.14 KB
/
No221.maximal-square.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Difficulty:
* Medium
*
* Desc:
* Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
*
* Example:
* Input:
* 1 0 1 0 0
* 1 0 1 1 1
* 1 1 1 1 1
* 1 0 0 1 0
* Output: 4
*/
public class Solution {
public int MaximalSquare(char[][] matrix) {
if (matrix.Length == 0) return 0;
int[,] dp = new int[matrix.Length + 1, matrix[0].Length + 1];
int maxLen = 0;
for (int i = 0; i < matrix.Length; i += 1) {
for (int j = 0; j < matrix[i].Length; j += 1) {
if (matrix[i][j] == '0') {
dp[i + 1, j + 1] = 0;
} else {
int left = dp[i + 1, j];
int top = dp[i, j + 1];
if (top == left) {
dp[i + 1, j + 1] = matrix[i - top][j - top] == '1' ? top + 1 : top;
} else {
dp[i + 1, j + 1] = Math.Min(top, left) + 1;
}
}
maxLen = Math.Max(maxLen, dp[i + 1, j + 1]);
}
}
return maxLen * maxLen;
}
}