Maximum Matrix Score After Flips

You are given an m x n binary matrix grid.

You may perform the following operation any number of times:

  • Choose any row or column
  • Flip every value in it
  • Flipping means changing 0 to 1 and 1 to 0

Each row of the final matrix is treated as a binary number, where the leftmost element is the most significant bit.

The score of the matrix is the sum of the binary values of all rows.

Return the maximum possible score that can be obtained after performing any number of flips.

Example 1:

Input: grid = [[0,1],[1,1]]

Output: 5

Explanation:

Flip the first row: [[1,0],[1,1]]

Now the rows represent:

  • 10 in binary = 2
  • 11 in binary = 3

Total score = 2 + 3 = 5

Example 2:

Input: grid = [[0,0,0],[1,0,1]]

Output: 12

Explanation:

Flip the first row: [[1,1,1],[1,0,1]]

Now the rows represent:

  • 111 in binary = 7
  • 101 in binary = 5

Total score = 7 + 5 = 12

Now Your Turn!

Pick the correct output for the given input

Input: grid = [[0,1,1],[1,0,0]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • grid.length == m
  • grid[i].length == n
  • 1 <= m, n <= 20
  • grid[i][j] is either 0 or 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int matrixScore(vector<vector<int>> &grid) {
// Your code goes here
}
};
Test Case

Input:

Grid