20250322 finished.

This commit is contained in:
jackfiled 2025-03-22 14:37:27 +08:00
parent 12e28d075a
commit 3846eebd59
2 changed files with 50 additions and 0 deletions

View File

@ -552,3 +552,5 @@ mod p2610_convert_an_array_into_a_2d_array_with_conditions;
mod p2612_minimum_reverse_operations; mod p2612_minimum_reverse_operations;
mod p2680_maximum_or; mod p2680_maximum_or;
mod p2643_row_with_maximum_ones;

View File

@ -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<i32>>) -> Vec<i32> {
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]])
);
}
}