Maximal Square LeetCode solution in Rust

1
use std::cmp;
2

3
impl Solution {
4
pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
5
let rows = matrix.len();
6
let cols = matrix[0].len();
7
let mut dp = vec![vec![0; cols + 1]; rows + 1];
8
let mut max_len: i32 = 0;
9

10
for row in 1..=rows {
11
for col in 1..=cols {
12
if matrix[row - 1][col - 1] == '1' {
13
dp[row][col] = 1
14
+ cmp::min(
15
cmp::min(dp[row - 1][col - 1], dp[row - 1][col]),
16
dp[row][col - 1],
17
);
18
max_len = cmp::max(max_len, dp[row][col] as i32);
19
}
20
}
21
}
22

23
max_len * max_len
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0