2397. Maximum Rows Covered by Columns Medium
1/**
2 * [2397] Maximum Rows Covered by Columns
3 *
4 * You are given a 0-indexed m x n binary matrix matrix and an integer numSelect, which denotes the number of distinct columns you must select from matrix.
5 * Let us consider s = {c1, c2, ...., cnumSelect} as the set of columns selected by you. A row row is covered by s if:
6 *
7 * For each cell matrix[row][col] (0 <= col <= n - 1) where matrix[row][col] == 1, col is present in s or,
8 * No cell in row has a value of 1.
9 *
10 * You need to choose numSelect columns such that the number of rows that are covered is maximized.
11 * Return the maximum number of rows that can be covered by a set of numSelect columns.
12 *
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered.png" style="width: 240px; height: 400px;" />
15 * Input: matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2
16 * Output: 3
17 * Explanation: One possible way to cover 3 rows is shown in the diagram above.
18 * We choose s = {0, 2}.
19 * - Row 0 is covered because it has no occurrences of 1.
20 * - Row 1 is covered because the columns with value 1, i.e. 0 and 2 are present in s.
21 * - Row 2 is not covered because matrix[2][1] == 1 but 1 is not present in s.
22 * - Row 3 is covered because matrix[2][2] == 1 and 2 is present in s.
23 * Thus, we can cover three rows.
24 * Note that s = {1, 2} will also cover 3 rows, but it can be shown that no more than three rows can be covered.
25 *
26 * Example 2:
27 * <img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered2.png" style="height: 250px; width: 84px;" />
28 * Input: matrix = [[1],[0]], numSelect = 1
29 * Output: 2
30 * Explanation: Selecting the only column will result in both rows being covered since the entire matrix is selected.
31 * Therefore, we return 2.
32 *
33 *
34 * Constraints:
35 *
36 * m == matrix.length
37 * n == matrix[i].length
38 * 1 <= m, n <= 12
39 * matrix[i][j] is either 0 or 1.
40 * 1 <= numSelect <= n
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/maximum-rows-covered-by-columns/
46// discuss: https://leetcode.com/problems/maximum-rows-covered-by-columns/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn maximum_rows(matrix: Vec<Vec<i32>>, num_select: i32) -> i32 {
52 0
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2397() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.