Source: LeetCode Link: https://leetcode-cn.com/problems/score-after-flipping-matrix
Statement: If I violate anyone’s rights, please contact me and I will delete it.
Welcome experts to spray I
Article Directory
topic
There is a two-dimensional matrix A in which the value of each element is 0 or 1.
Moving refers to selecting any row or column and converting each value in that row or column: changing all 0s to 1 and all 1s to 0.
After making any number of moves, each row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.
Return the highest possible score.
Example:
Input: [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output: 39
Explanation:
Convert to [[1,1,1, 1],[1,0,0,1],[1,1,1,1]]
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
提示:
1 <= A.length <= 20
1 <= A[0].length <= 20
A[i][j] 是 0 或 1
My code
The idea is the same as the official one:
the leftmost column in the matrix should be all 1, and the rest should be as many as possible.
- Step 1: Set all the first columns to 1, ensure that all the highest bits are fetched, and all rows that are not 1 in the first column are flipped
- Step 2: Starting from the second column, flip the rows in which the number of 1s is less than 0 in all columns, and ensure that the number of 1s is as large as possible
- Step 3: Return the calculation result
class Solution {
public int matrixScore(int[][] A) {
int row = A.length, col = A[0].length;
//将每一列的首个数字变为1
for(int i=0;i<row;++i){
if(A[i][0] == 0){
changerow(A, i);
}
}
//System.out.println(Arrays.deepToString(A));
//遍历每一列,1的个数小于0的个数就需要改变
for(int i=1;i<col;++i){
int count0 = 0, count1 = 0;
for(int j=0;j<row;++j){
if(A[j][i] == 0) count0 ++;
else count1 ++;
}
if(count0 > count1){
changecol(A, i);
}
}
//System.out.println(Arrays.deepToString(A));
int ret=0;
for(int i =0;i<row;++i){
int tem = 0;
int index = 1;
for(int j=col-1;j>=0;--j){
tem += A[i][j] * index;
index = index * 2;
}
ret += tem;
}
return ret;
}
public void changecol(int[][] arr, int col){
int row = arr.length;
for(int i=0;i<row;++i){
if(arr[i][col] == 0){
arr[i][col] = 1;
}else arr[i][col] = 0;
}
}
public void changerow(int[][]arr, int row){
int col = arr[row].length;
for(int i=0;i<col;++i){
if(arr[row][i]==0) arr[row][i] =1;
else arr[row][i] = 0;
}
}
}
Official code:
Author: LeetCode-Solution
link: https: //leetcode-cn.com/problems/score-after-flipping-matrix/solution/fan-zhuan-ju-zhen-hou-de-de-fen-by-leetc-cxma /
Calculated column by column For
example:
[[0,0,1,1],
[1,0,1,0],
[1,1,0,0]]
- First calculate the first column, because we want to change all the first column to 1, so the sum of the first column is m * (1 << (n-1)), where m is the number of rows and n-1 is the number of columns- 1, where n-1=3, shift 1 to the right by 3 places, which is 1000, 1000, 1000,
- Then calculate 1-n-1 columns separately, when calculating 1 column, check whether the first bit of each row is 0, if it is 1, no need to flip, if it is 0, you need to flip 1-A[i][j], Then the sum of the columns with subscript j here needs to be shifted by n-1-j bits to the right: k * (1 << (n-j-1))
- int k = Math.max(nOnes, m-nOnes); The meaning of taking the maximum value is to ensure that each column calculates the maximum number of 1, such as the third column. Follow the steps to calculate nOnes=1, which is less than half of the number of rows. So this column needs to be flipped. After flipping, nOnes=3-1=2, this is the number of 1 at most
class Solution {
public int matrixScore(int[][] A) {
int m = A.length, n = A[0].length;
int ret = m * (1 << (n - 1));
for (int j = 1; j < n; j++) {
int nOnes = 0;
for (int i = 0; i < m; i++) {
if (A[i][0] == 1) {
nOnes += A[i][j];
} else {
nOnes += (1 - A[i][j]); // 如果这一行进行了行反转,则该元素的实际取值为 1 - A[i][j]
}
}
int k = Math.max(nOnes, m - nOnes);
ret += k * (1 << (n - j - 1));
}
return ret;
}
}