Score After Flipping Matrix

Medium

Description

Given a binary matrix, you may toggle any row or column. Return the maximum possible score, where each row is a binary number.

Examples

Input:grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output:39
Explanation:

Optimal flips maximize sum.

Input:grid = [[1]]
Output:1
Explanation:

Minimal case with a single-element matrix.

Input:grid = [[1,0,1],[0,1,0],[1,0,1]]
Output:21
Explanation:

Initial matrix represents binary numbers: row 0 = 101 (5), row 1 = 010 (2), row 2 = 101 (5), sum = 12. To maximize, the goal is leftmost bits to be 1. Toggle column 1 to get [[1,1,1],[0,0,0],[1,1,1]]. Now row 0 = 111 (7), row 1 = 000 (0), row 2 = 111 (7). Toggle row 1 to get [[1,1,1],[1,1,1],[1,1,1]]. Final sum: 7 + 7 + 7 = 21.

Constraints

  • 1 ≤ m, n ≤ 20

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!