> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Score After Flipping Matrix Python Solution

> Tested Python solution for LeetCode 861 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 861, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Bit Manipulation](/catalog/topics/bit-manipulation), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/score-after-flipping-matrix/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 861   # by problem number
lcpy gen -s score_after_flipping_matrix   # by problem name
```

## Problem

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)*.

### Examples

```
Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output: 39
Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
```

```
Input: grid = [[0]]
Output: 1
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/score_after_flipping_matrix/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/score_after_flipping_matrix/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(1)
    def matrix_score(self, grid: list[list[int]]) -> int:
        m, n = len(grid), len(grid[0])
        score = 0
        for j in range(n):
            # After the optimal row flip, a row's bit at j is 1 exactly when
            # grid[i][j] == grid[i][0] (the leading bit is always set to 1)
            ones = sum(1 for row in grid if row[j] == row[0])
            score += max(ones, m - ones) * (1 << (n - 1 - j))
        return score
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.