Skip to content

Commit 74545f5

Browse files
authored
Create 0221-maximal-square.kt
1 parent 874de3d commit 74545f5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

kotlin/0221-maximal-square.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
fun maximalSquare(g: Array<CharArray>): Int {
3+
var dp = Array(g.size) { IntArray(g[0].size)}
4+
5+
val m = g.lastIndex
6+
val n = g[0].lastIndex
7+
var res = 0
8+
for (i in m downTo 0) {
9+
for (j in n downTo 0) {
10+
if (g[i][j] == '0')
11+
continue
12+
13+
dp[i][j] = 1 + minOf(
14+
if (i < m && j < n) dp[i + 1][j + 1] else 0,
15+
if (i < m) dp[i + 1][j] else 0,
16+
if (j < n) dp[i][j + 1] else 0
17+
)
18+
19+
res = maxOf(
20+
res,
21+
dp[i][j]
22+
)
23+
}
24+
}
25+
26+
return res * res
27+
}
28+
}

0 commit comments

Comments
 (0)