Score After Flipping Matrix

Medium Topics Company Tags

You may flip any whole row or whole column of the binary grid, replacing each 0 with 1 and each 1 with 0. Interpret every row as a binary integer, with its leftmost entry as the most significant bit, and add these integers to obtain the score. Return the largest score achievable after any number of flips, including none.

Example 1:

Score After Flipping Matrix, example 1: illustration of the example

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

Output: 6

Explanation: Flip the first row to obtain [[1,0],[1,0]], then flip the second column to obtain [[1,1],[1,1]]. Both rows represent 3, giving the maximum score 6.

Example 2:

Score After Flipping Matrix, example 2: illustration of the example

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

Output: 5

Explanation: The rows already represent 2 and 3. A score of 6 would require both rows to be 11, but row and column flips cannot make these two rows identical. The maximum is 5.

Example 3:

Score After Flipping Matrix, example 3: illustration of the example

Input: grid = [[0]]

Output: 1

Explanation: Flip the only row or column so its entry becomes 1. The resulting score is 1.

Constraints:

  • 1 <= grid.length <= 20
  • Every row has the same length, between 1 and 20.
  • Every entry is either 0 or 1.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.