-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalSquare.java
More file actions
44 lines (38 loc) · 1.21 KB
/
Copy pathMaximalSquare.java
File metadata and controls
44 lines (38 loc) · 1.21 KB
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
44
/*
* File Name:MaximalSquare is created on 2020/9/20 11:10 下午 by lite
*
* Copyright (c) 2020, xiaoyujiaoyu technology All Rights Reserved.
*
*/
/**
* @author lite
* @Description:
* 要形成正方形
* 1.当前 上 左 左上都是1
* 2.当前 上 左 左上都不受0限制才能成正方形
* if (grid[i - 1][j - 1] == '1') {
* dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1;
* }
* @date: 2020/9/20 11:10 下午
* @since JDK 1.8
*/
public class MaximalSquare {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return 0;
}
int height = matrix.length;
int width = matrix[0].length;
int maxSide = 0;
int[][] dp = new int[height + 1][width + 1];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (matrix[row][col] == '1') {
dp[row + 1][col + 1] = Math.min(Math.min(dp[row + 1][col], dp[row][col + 1]), dp[row][col]) + 1;
maxSide = Math.max(maxSide, dp[row + 1][col + 1]);
}
}
}
return maxSide * maxSide;
}
}