Skip to content

Commit

Permalink
Update p221
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaHuaY committed Apr 19, 2024
1 parent 730570f commit 41185d5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Call test by running `cargo test p[xxx]_`.
- [2864. Maximum Odd Binary Number](src/leetcode/p2864_maximum_odd_binary_number)
- [2917. Find the K-or of an Array](src/leetcode/p2917_find_the_k_or_of_an_array)

### Medium(182)
### Medium(183)

- [2. Add Two Numbers](src/leetcode/p2_add_two_numbers)
- [3. Longest Substring Without Repeating Characters](src/leetcode/p3_longest_substring_without_repeating_characters)
Expand Down Expand Up @@ -171,6 +171,7 @@ Call test by running `cargo test p[xxx]_`.
- [150. Evaluate Reverse Polish Notation](src/leetcode/p150_evaluate_reverse_polish_notation)
- [165. Compare Version Numbers](src/leetcode/p165_compare_version_numbers)
- [173. Binary Search Tree Iterator](src/leetcode/p173_binary_search_tree_iterator)
- [221. Maximal Square](src/leetcode/p221_maximal_square)
- [227. Basic Calculator II](src/leetcode/p227_basic_calculator_ii)
- [235. Lowest Common Ancestor of a Binary Search Tree](src/leetcode/p235_lowest_common_ancestor_of_a_binary_search_tree)
- [236. Lowest Common Ancestor of a Binary Tree](src/leetcode/p236_lowest_common_ancestor_of_a_binary_tree)
Expand Down
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub mod p2171_removing_minimum_number_of_magic_beans;
pub mod p2182_construct_string_with_repeat_limit;
pub mod p2192_all_ancestors_of_a_node_in_a_directed_acyclic_graph;
pub mod p21_merge_two_sorted_lists;
pub mod p221_maximal_square;
pub mod p224_basic_calculator;
pub mod p225_implement_stack_using_queues;
pub mod p227_basic_calculator_ii;
Expand Down
71 changes: 71 additions & 0 deletions src/leetcode/p221_maximal_square/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
pub struct Solution;

impl Solution {
pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
let n = matrix[0].len();
let mut dp = vec![0i32; n];
let mut dp_next = dp.clone();
let mut result = 0;
for (i, row) in matrix.into_iter().enumerate() {
if i == 0 {
for (j, c) in row.into_iter().enumerate() {
if c == '1' {
dp[j] = 1;
result = 1;
}
}
} else {
for (j, c) in row.into_iter().enumerate() {
if c == '1' {
if j == 0 {
dp_next[j] = 1;
result = result.max(1);
} else {
dp_next[j] = dp_next[j - 1].min(dp[j - 1]).min(dp[j]) + 1;
result = result.max(dp_next[j] * dp_next[j]);
}
} else {
dp_next[j] = 0;
}
}
std::mem::swap(&mut dp, &mut dp_next);
}
}
result
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::common::ToVecVecChar;

#[test]
fn test1() {
assert_eq!(
Solution::maximal_square(
[
["1", "0", "1", "0", "0"],
["1", "0", "1", "1", "1"],
["1", "1", "1", "1", "1"],
["1", "0", "0", "1", "0"]
]
.to_vec_vec_char()
),
4
);
}

#[test]
fn test2() {
assert_eq!(
Solution::maximal_square([["0", "1"], ["1", "0"]].to_vec_vec_char()),
1
);
}

#[test]
fn test3() {
assert_eq!(Solution::maximal_square([["0"]].to_vec_vec_char()), 0);
}
}

0 comments on commit 41185d5

Please sign in to comment.