-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
74 additions
and
0 deletions.
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,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 | ||
} | ||
} |
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
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 @@ | ||
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); | ||
} |
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