Leetcode 2397: Maximum Rows Covered by Columns

grid47
grid47
Exploring patterns and algorithms
Mar 12, 2024 7 min read

You are given a binary matrix matrix of size m x n and an integer numSelect. Your goal is to select exactly numSelect distinct columns from the matrix such that you cover as many rows as possible. A row is considered covered if all the 1’s in that row are included in the selected columns. If a row has no 1’s, it is also considered covered.
Problem
Approach
Steps
Complexity
Input: The input consists of an m x n binary matrix `matrix` and an integer `numSelect` representing the number of columns to select.
Example: matrix = [[0, 0, 0], [1, 0, 1], [0, 1, 1], [0, 0, 1]], numSelect = 2
Constraints:
• 1 <= m, n <= 12
• 1 <= numSelect <= n
Output: Return the maximum number of rows that can be covered by selecting `numSelect` columns.
Example: Output: 3
Constraints:
Goal: The goal is to select `numSelect` columns such that the maximum number of rows are covered. A row is covered if all its 1's appear in the selected columns.
Steps:
• 1. Iterate through all possible selections of `numSelect` columns.
• 2. For each selection, check if the columns cover the rows based on the presence of 1's in the selected columns.
• 3. Count the number of rows that are covered.
• 4. Keep track of the maximum number of rows covered across all column selections.
Goal: The solution must efficiently handle all possible column selections for the given matrix size.
Steps:
• m, n <= 12 ensures that the problem size is small enough to try all possible column selections.
Assumptions:
• Matrix entries are either 0 or 1.
• numSelect is at most n, the number of columns in the matrix.
Input: matrix = [[1, 0], [0, 0], [1, 1]], numSelect = 1
Explanation: Selecting column 1 will cover both rows with 1's. Therefore, the answer is 3, as all rows are covered.

Input: matrix = [[0, 0, 0], [1, 0, 1], [0, 1, 1], [0, 0, 1]], numSelect = 2
Explanation: Selecting columns 0 and 2 will cover 3 rows because: Row 1 is covered as both columns 0 and 2 are selected, Row 0 is covered as it has no 1's, Row 3 is covered as column 2 is selected. The maximum rows covered is 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus