Skip to content

Commit

Permalink
add: 被列覆盖的最多行数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 4, 2024
1 parent 9230254 commit a82833d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,7 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8
- [一周中的第几天](src/math/day_of_the_week.rs) [数学]

- LeetCode 1185. 一周中的第几天 <https://leetcode.cn/problems/day-of-the-week>

- [被列覆盖的最多行数](src/math/maximum_rows_covered_by_columns.rs) [位运算, 数组, 回溯, 枚举, 矩阵]

- LeetCode 2397. 被列覆盖的最多行数 <https://leetcode.cn/problems/maximum-rows-covered-by-columns>
Binary file added images/math/maximum_rows_covered_by_columns.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions src/math/maximum_rows_covered_by_columns.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<i32>>, 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
}
}
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
30 changes: 30 additions & 0 deletions tests/math/maximum_rows_covered_by_columns_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions tests/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit a82833d

Please sign in to comment.