-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreAfterFlippingMatrix861.java
37 lines (30 loc) · 1.13 KB
/
ScoreAfterFlippingMatrix861.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class ScoreAfterFlippingMatrix861 {
/*
* You are given an m x n binary matrix grid.
*
* A move consists of choosing any row or column and toggling each value in that row or column
* (i.e., changing all 0's to 1's, and all 1's to 0's).
*
* Every 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 after making any number of moves (including zero moves).
*/
class Solution {
public int matrixScore(int[][] grid) {
int n = grid.length ;
int m = grid[0].length ;
int score = (1 <<(m-1)) * n ;
for (int i = 1; i < m; i++) {
int count = 0 ;
for (int j = 1; j < n; j++) {
if (grid[j][i] == grid[j][0]) count++;
}
count = Math.max(count,n -count);
int colScore = (1 << (n - i - 1)) ;
score += colScore;
}
return score ;
}
}
}