From 3846eebd5919652bf691647a8ad00475a718db09 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 22 Mar 2025 14:37:27 +0800 Subject: [PATCH] 20250322 finished. --- src/problem/mod.rs | 2 + src/problem/p2643_row_with_maximum_ones.rs | 48 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/problem/p2643_row_with_maximum_ones.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 44a1816..12f903c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -552,3 +552,5 @@ mod p2610_convert_an_array_into_a_2d_array_with_conditions; mod p2612_minimum_reverse_operations; mod p2680_maximum_or; + +mod p2643_row_with_maximum_ones; diff --git a/src/problem/p2643_row_with_maximum_ones.rs b/src/problem/p2643_row_with_maximum_ones.rs new file mode 100644 index 0000000..b16058f --- /dev/null +++ b/src/problem/p2643_row_with_maximum_ones.rs @@ -0,0 +1,48 @@ +/** + * [2643] Row With Maximum Ones + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn row_and_maximum_ones(mat: Vec>) -> Vec { + let r = mat.iter().enumerate().rev().fold( + (mat.len() - 1, 0), + |(r_line, r_count), (line, row)| { + let c = row.iter().filter(|x| **x == 1).count(); + + if c >= r_count { + (line, c) + } else { + (r_line, r_count) + } + }, + ); + + vec![r.0 as i32, r.1 as i32] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2643() { + assert_eq!( + vec![0, 1], + Solution::row_and_maximum_ones(vec![vec![0, 1], vec![1, 0]]) + ); + assert_eq!( + vec![1, 2], + Solution::row_and_maximum_ones(vec![vec![0, 0, 0], vec![0, 1, 1]]) + ); + assert_eq!( + vec![1, 2], + Solution::row_and_maximum_ones(vec![vec![0, 0], vec![1, 1], vec![0, 0]]) + ); + } +}