-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |