From 00e3da6d1df6aaf81756da06b7fccfcf1c1be659 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 15 Nov 2024 11:02:45 +0800 Subject: [PATCH] 20241115 finished. --- src/problem/mod.rs | 2 + ...flips_to_make_binary_grid_palindromic_i.rs | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/problem/p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5c58bd5..9d47ac9 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -316,3 +316,5 @@ mod p3258_count_substrings_that_satisfy_k_constraint_i; mod p3261_count_substrings_that_satisfy_k_constraint_ii; mod p3249_count_the_number_of_good_nodes; + +mod p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i; diff --git a/src/problem/p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i.rs b/src/problem/p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i.rs new file mode 100644 index 0000000..565b9d3 --- /dev/null +++ b/src/problem/p3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i.rs @@ -0,0 +1,52 @@ +/** + * [3239] Minimum Number of Flips to Make Binary Grid Palindromic I + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn min_flips(grid: Vec>) -> i32 { + let (m, n) = (grid.len(), grid[0].len()); + let mut row = 0; + + for i in 0..m { + for j in 0..n / 2 { + if grid[i][j] != grid[i][n - j - 1] { + row += 1; + } + } + } + + let mut column = 0; + + for i in 0..n { + for j in 0..m / 2 { + if grid[j][i] != grid[m - j - 1][i] { + column += 1; + } + } + } + + row.min(column) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3239() { + assert_eq!( + 2, + Solution::min_flips(vec![vec![1, 0, 0], vec![0, 0, 0], vec![0, 0, 1]]) + ); + assert_eq!( + 1, + Solution::min_flips(vec![vec![0, 1], vec![0, 1], vec![0, 0]]) + ); + } +}