20240815 Finished

This commit is contained in:
jackfiled 2024-08-15 12:59:10 +08:00
parent c7a090afa4
commit 2da0c3af3b
2 changed files with 48 additions and 1 deletions

View File

@ -204,4 +204,5 @@ mod p63_unique_paths_ii;
mod p97_interleaving_string;
mod p72_edit_distance;
mod p123_best_time_to_buy_and_sell_stock_iii;
mod p188_best_time_to_buy_and_sell_stock_iv;
mod p188_best_time_to_buy_and_sell_stock_iv;
mod p221_maximal_square;

View File

@ -0,0 +1,46 @@
/**
* [221] Maximal Square
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
let (m, n) = (matrix.len(), matrix[0].len());
let mut dp = vec![vec![0; n]; m];
let mut result: i32 = 0;
for i in 0..m {
for j in 0..n {
if matrix[i][j] == '0' {
continue;
}
if i == 0 || j == 0 {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i - 1][j - 1].min(dp[i - 1][j]).min(dp[i][j - 1]) + 1;
}
result = result.max(dp[i][j]);
}
}
result.pow(2)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_221() {
assert_eq!(1, Solution::maximal_square(vec![vec!['0', '1'], vec!['1', '0']]));
assert_eq!(0, Solution::maximal_square(vec![vec!['0']]));
}
}