Flip Columns For Maximum Number of Equal Rows

Medium Topics Company Tags

Choose any set of columns of the binary matrix and flip every entry in each chosen column. A row counts if all entries within that row are equal after the flips: it may be all zeros or all ones. Different counted rows do not need to have the same bit. Return the largest number of rows that can count after applying one shared set of column flips to the entire matrix.

Example 1:

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

Output: 2

Explanation: Flip the middle column. The first two rows become [1,1,1] and [0,0,0], so both count even though their bits differ. The third row is not uniform, leaving a maximum of 2.

Example 2:

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

Output: 3

Explanation: Each row has one entry and is already uniform. All 3 count without any flips.

Example 3:

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

Output: 2

Explanation: Without flips, the first two rows are uniform. Making the last row uniform would make the first two non-uniform, so the maximum count is 2.

Constraints:

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


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.