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 14569cc commit 5241b54
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,7 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
- [一周中的第几天](src/math/day_of_the_week.cpp) [数学]

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

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

- 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.
34 changes: 34 additions & 0 deletions src/math/maximum_rows_covered_by_columns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 被列覆盖的最多行数
// https://leetcode.cn/problems/maximum-rows-covered-by-columns
// INLINE ../../images/math/maximum_rows_covered_by_columns.jpeg

#include <headers.hpp>

class Solution {
public:
int maximumRows(vector<vector<int>> &matrix, int numSelect) {
int m = matrix.size(), n = matrix[0].size();
vector<int> mask(m, 0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mask[i] += matrix[i][j] << (n - j - 1);
}
}
int res = 0;
int cur = 0;
int limit = (1 << n);
while ((++cur) < limit) {
if (__builtin_popcount(cur) != numSelect) {
continue;
}
int t = 0;
for (int j = 0; j < m; j++) {
if ((mask[j] & cur) == mask[j]) {
++t;
}
}
res = max(res, t);
}
return res;
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-03 10:37:56
// 执行编译时间:2024-01-04 09:37:38
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
30 changes: 30 additions & 0 deletions test/math/maximum_rows_covered_by_columns_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <math/maximum_rows_covered_by_columns.cpp>

TEST(被列覆盖的最多行数, maximumRows) {
Solution solution;
// 示例 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 行,但可以证明无法覆盖更多行。
vector<vector<int>> matrix = {{0, 0, 0}, {1, 0, 1}, {0, 1, 1}, {0, 0, 1}};
int numSelect = 2;
EXPECT_EQ(solution.maximumRows(matrix, numSelect), 3);

// 示例 2:
// 输入:matrix = [[1],[0]], numSelect = 1
// 输出:2
// 解释:
// 选择唯一的一列,两行都被覆盖了,因为整个矩阵都被覆盖了。
// 所以我们返回 2 。
matrix = {{1}, {0}};
numSelect = 1;
EXPECT_EQ(solution.maximumRows(matrix, numSelect), 2);
}

0 comments on commit 5241b54

Please sign in to comment.