通过万岁!!!
java代码
class Solution {
int n, m, max = Integer.MIN_VALUE;
int[][] myMatrix;
public int maximumRows(int[][] matrix, int numSelect) {
myMatrix = matrix;
n = matrix.length;
m = matrix[0].length;
int[] choice = new int[m];
fun(choice, numSelect, 0);
return max;
}
public void fun(int[] choice, int numSelect, int begin) {
if (numSelect == 0) {
max = Math.max(max, computerRowNum(choice));
return;
}
for (int i = begin; i < m && numSelect <= m - i; i++) {
if (choice[i] == 1) {
continue;
}
choice[i] = 1;
fun(choice, numSelect - 1, i + 1);
choice[i] = 0;
}
}
public int computerRowNum(int[] choice) {
Set<Integer> existRow = new HashSet<>();
// 不选的列中,那些行是1
for (int i = 0; i < m; i++) {
if (choice[i] == 0) {
for (int j = 0; j < n; j++) {
if (myMatrix[j][i] == 1) {
existRow.add(j);
}
}
}
}
return n - existRow.size();
}
}