diff --git a/README.md b/README.md index 92624d9..570e95b 100644 --- a/README.md +++ b/README.md @@ -752,3 +752,7 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8 - [一周中的第几天](src/math/day_of_the_week.rs) [数学] - LeetCode 1185. 一周中的第几天 + +- [被列覆盖的最多行数](src/math/maximum_rows_covered_by_columns.rs) [位运算, 数组, 回溯, 枚举, 矩阵] + + - LeetCode 2397. 被列覆盖的最多行数 diff --git a/images/math/maximum_rows_covered_by_columns.jpeg b/images/math/maximum_rows_covered_by_columns.jpeg new file mode 100644 index 0000000..bda9bc5 Binary files /dev/null and b/images/math/maximum_rows_covered_by_columns.jpeg differ diff --git a/src/math/maximum_rows_covered_by_columns.rs b/src/math/maximum_rows_covered_by_columns.rs new file mode 100644 index 0000000..9c4fd8d --- /dev/null +++ b/src/math/maximum_rows_covered_by_columns.rs @@ -0,0 +1,38 @@ +// 被列覆盖的最多行数 +// https://leetcode.cn/problems/maximum-rows-covered-by-columns +// INLINE ../../images/math/maximum_rows_covered_by_columns.jpeg + +pub struct Solution; + +impl Solution { + pub fn maximum_rows(matrix: Vec>, num_select: i32) -> i32 { + let m = matrix.len(); + let n = matrix[0].len(); + + let mut mask = vec![0; m]; + for i in 0..m { + for j in 0..n { + mask[i] += matrix[i][j] << (n - j - 1); + } + } + + let mut res = 0; + let mut cur: u32 = 0; + let limit = 1 << n; + while (cur + 1) < limit { + cur += 1; + if cur.count_ones() != num_select as u32 { + continue; + } + let mut t = 0; + for j in 0..m { + if (mask[j] as u32 & cur) == mask[j] as u32 { + t += 1; + } + } + res = res.max(t); + } + + res + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 0aff0db..b12cad3 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -19,3 +19,4 @@ pub mod next_greater_numerically_balanced_number; pub mod number_of_burgers_with_no_waste_of_ingredients; pub mod maximum_students_taking_exam; pub mod day_of_the_week; +pub mod maximum_rows_covered_by_columns; diff --git a/tests/math/maximum_rows_covered_by_columns_test.rs b/tests/math/maximum_rows_covered_by_columns_test.rs new file mode 100644 index 0000000..6897db3 --- /dev/null +++ b/tests/math/maximum_rows_covered_by_columns_test.rs @@ -0,0 +1,30 @@ +use rust_practice::math::maximum_rows_covered_by_columns::Solution; + +#[test] +fn maximum_rows() { + // 示例 1: + // 输入:matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2 + // 输出:3 + // 解释: + // 图示中显示了一种覆盖 3 行的可行办法。 + // 选择 s = {0, 2} 。 + // - 第 0 行被覆盖,因为其中没有出现 1 。 + // - 第 1 行被覆盖,因为值为 1 的两列(即 0 和 2)均存在于 s 中。 + // - 第 2 行未被覆盖,因为 matrix[2][1] == 1 但是 1 未存在于 s 中。 + // - 第 3 行被覆盖,因为 matrix[2][2] == 1 且 2 存在于 s 中。 + // 因此,可以覆盖 3 行。 + // 另外 s = {1, 2} 也可以覆盖 3 行,但可以证明无法覆盖更多行。 + let matrix = vec![vec![0, 0, 0], vec![1, 0, 1], vec![0, 1, 1], vec![0, 0, 1]]; + let num_select = 2; + assert_eq!(Solution::maximum_rows(matrix, num_select), 3); + + // 示例 2: + // 输入:matrix = [[1],[0]], numSelect = 1 + // 输出:2 + // 解释: + // 选择唯一的一列,两行都被覆盖了,因为整个矩阵都被覆盖了。 + // 所以我们返回 2 。 + let matrix = vec![vec![1], vec![0]]; + let num_select = 1; + assert_eq!(Solution::maximum_rows(matrix, num_select), 2); +} diff --git a/tests/math/mod.rs b/tests/math/mod.rs index 99fdede..8554bd8 100644 --- a/tests/math/mod.rs +++ b/tests/math/mod.rs @@ -19,3 +19,4 @@ pub mod next_greater_numerically_balanced_number_test; pub mod number_of_burgers_with_no_waste_of_ingredients_test; pub mod maximum_students_taking_exam_test; pub mod day_of_the_week_test; +pub mod maximum_rows_covered_by_columns_test;