LeetCode 题解工作台

按列翻转得到最大值等行数

给定 m x n 矩阵 matrix 。 你可以从中选出任意数量的列并翻转其上的 每个 单元格。(即翻转后,单元格的值从 0 变成 1 ,或者从 1 变为 0 。) 返回 经过一些翻转后,行内所有值都相等的最大行数 。 示例 1: 输入: matrix = [[0,1],[1,1]] 输出: 1 解…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们观察发现,如果矩阵中的两行满足以下条件之一,则它们可以通过翻转某些列的方式得到相等的行: 1. 两行的对应位置元素相等,即如果其中一行元素为 ,则另一行元素也为 ;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定 m x n 矩阵 matrix 。

你可以从中选出任意数量的列并翻转其上的 每个 单元格。(即翻转后,单元格的值从 0 变成 1,或者从 1 变为 0 。)

返回 经过一些翻转后,行内所有值都相等的最大行数 。

 

示例 1:

输入:matrix = [[0,1],[1,1]]
输出:1
解释:不进行翻转,有 1 行所有值都相等。

示例 2:

输入:matrix = [[0,1],[1,0]]
输出:2
解释:翻转第一列的值之后,这两行都由相等的值组成。

示例 3:

输入:matrix = [[0,0,0],[0,0,1],[1,1,0]]
输出:2
解释:翻转前两列的值之后,后两行由相等的值组成。

 

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j] == 0 或 1
lightbulb

解题思路

方法一:哈希表

我们观察发现,如果矩阵中的两行满足以下条件之一,则它们可以通过翻转某些列的方式得到相等的行:

  1. 两行的对应位置元素相等,即如果其中一行元素为 1,0,0,11,0,0,1,则另一行元素也为 1,0,0,11,0,0,1
  2. 两行的对应位置元素相反,即如果其中一行元素为 1,0,0,11,0,0,1,则另一行元素为 0,1,1,00,1,1,0

我们称满足以上条件之一的两行元素为“等价行”,那么题目所求的答案即为矩阵中最多包含等价行的行数。

因此,我们可以遍历矩阵的每一行,将每一行转换成第一个元素为 00 的“等价行”。具体做法如下:

  • 如果当前行的第一个元素为 00,那么当前行的元素保持不变;
  • 如果当前行的第一个元素为 11,那么我们将当前行的每个元素进行翻转,即 00 变成 11, 11 变成 00。也就是说,我们将以 11 开头的行翻转成以 00 开头的“等价行”。

这样一来,我们只需要用一个哈希表来统计转换后的每一行的出现次数,其中键为转换后的行(可以将所有数字拼接成一个字符串),值为该行出现的次数。最后,哈希表中值的最大值即为矩阵中最多包含等价行的行数。

时间复杂度 O(m×n)O(m \times n),空间复杂度 O(m)O(m)。其中 mmnn 分别是矩阵的行数和列数。

相似题目:

1
2
3
4
5
6
7
8
class Solution:
    def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
        cnt = Counter()
        for row in matrix:
            t = tuple(row) if row[0] == 0 else tuple(x ^ 1 for x in row)
            cnt[t] += 1
        return max(cnt.values())
speed

复杂度分析

指标
时间O(n \cdot m)
空间O(n \cdot m)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's understanding of binary operations and pattern matching within arrays.

  • question_mark

    Gauge the candidate's ability to utilize hash tables effectively for optimizing solutions.

  • question_mark

    Evaluate the candidate's approach to reducing unnecessary computations when flipping columns.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the XOR operation's effect on the row patterns when flipping columns.

  • error

    Overcomplicating the solution by trying to brute-force all possible column flips instead of using a hash table for optimization.

  • error

    Missing edge cases such as matrices where no flips are needed or where flipping results in fewer equal rows.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    How would the solution change if we could only flip a single column?

  • arrow_right_alt

    What if the matrix had more than two distinct values (e.g., 0, 1, and 2)?

  • arrow_right_alt

    Can the solution be optimized further using advanced techniques like bitwise manipulation or dynamic programming?

help

常见问题

外企场景

按列翻转得到最大值等行数题解:数组·哈希·扫描 | LeetCode #1072 中等