304 North Cardinal St.
Dorchester Center, MA 02124

# Maximum Rows Covered by Columns LeetCode Solution

## Problem – Maximum Rows Covered by Columns LeetCode Solution

You are given a 0-indexed `m x n` binary matrix `mat` and an integer `cols`, which denotes the number of columns you must choose.

A row is covered by a set of columns if each cell in the row that has a value of `1` also lies in one of the columns of the chosen set.

Return the maximum number of rows that can be covered by a set of `cols` columns.

Example 1:

``````Input: mat = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], cols = 2
Output: 3
Explanation:
As shown in the diagram above, one possible way of covering 3 rows is by selecting the 0th and 2nd columns.
It can be shown that no more than 3 rows can be covered, so we return 3.
``````

Example 2:

``````Input: mat = [,], cols = 1
Output: 2
Explanation:
Selecting the only column will result in both rows being covered, since the entire matrix is selected.
Therefore, we return 2.
``````

Constraints:

• `m == mat.length`
• `n == mat[i].length`
• `1 <= m, n <= 12`
• `mat[i][j]` is either `0` or `1`.
• `1 <= cols <= n`

## Maximum Rows Covered by Columns LeetCode Solution in C++

``````class Solution {
public:
int maxi = INT_MIN;
void helper(vector<vector<int>> &mat , int m , int n , int cols , int idx , vector<int> &vis){
if(cols == 0 or idx==n){
int cnt = 0;
for(int p = 0 ; p < m ; p++){
bool check = true;
for(int q = 0 ; q < n; q++){
// if cell is 1 and not visited then we cannot take this row
if(mat[p][q] == 1 and vis[q] == 0){
check = false;
break;
}
}
if(check) cnt++;
}
maxi = max(maxi,cnt);
return;
}

// picking idx th column and marking column as visited
vis[idx]=1;
helper(mat,m,n,cols-1,idx+1,vis);
vis[idx]=0;

// not picking
helper(mat,m,n,cols,idx+1,vis);
return;

}
int maximumRows(vector<vector<int>>& mat, int cols) {
int m = mat.size();
int n = mat.size();

vector<int> vis(n);

helper(mat,m,n,cols,0,vis);
return maxi;
}
};
``````

## Maximum Rows Covered by Columns LeetCode Solution in Java

``````    static int maximumRows(int[][] mat, int cols) {
return getResult(mat, cols, new HashSet<Integer>(),0) ;
}
static int getResult(int [][] mat, int cols, Set<Integer> set, int x){
int maxResult = 0;
if(cols == 0){
int count = 0;
for(int i = 0; i < mat.length; i++) {
int [] row = mat[i];
boolean flag = true;
for(int col = 0; col < row.length; col++){
if(row[col] == 1 && !set.contains(col)){
flag = false; break;
}
}
if(flag) count++;

}
return count;
}

for(int i = x; i < mat.length; i++) {
maxResult = Math.max(maxResult, getResult(mat, cols - 1, set, i+1));
set.remove(i);
}
return maxResult;
}
``````

## Maximum Rows Covered by Columns LeetCode Solution in Python

``````class Solution:
def maximumRows(self, mat: List[List[int]], cols: int) -> int:
n,m = len(mat),len(mat)
ans = 0

def check(state,row,rowIncludedCount):
nonlocal ans
if row==n:
if sum(state)<=cols:
ans = max(ans,rowIncludedCount)
return

check(state[::],row+1,rowIncludedCount)
for j in range(m):
if mat[row][j]==1:
state[j] = 1
check(state,row+1,rowIncludedCount+1)

check(*m,0,0)
return ans
``````
##### Maximum Rows Covered by Columns LeetCode Solution Review:

In our experience, we suggest you solve this Maximum Rows Covered by Columns LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Maximum Rows Covered by Columns LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Maximum Rows Covered by Columns LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions