[LeetCode] 1072. Flip Columns For Maximum Number of Equal Rows

You are given an m x n binary matrix matrix.

You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa).

Return the maximum number of rows that have all values equal after some number of flips.

Example 1:

Input: matrix = [[0,1],[1,1]]
Output: 1
Explanation: After flipping no values, 1 row has all values equal.

Example 2:

Input: matrix = [[0,1],[1,0]]
Output: 2
Explanation: After flipping values in the first column, both rows have equal values.

Example 3:

Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
Explanation: After flipping values in the first two columns, the last two rows have equal values.

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j] is either 0 or 1.

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

给定由若干 0 和 1 组成的矩阵 matrix,从中选出任意数量的列并翻转其上的 每个 单元格。翻转后,单元格的值从 0 变成 1,或者从 1 变为 0 。

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flip-columns-for-maximum-number-of-equal-rows
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的思路我参考了美版 discussion 的最高票答案

题目给的是一个只有 0 和 1 的矩阵,问你能否通过翻转任意列(竖着从上到下)里所有的单元格(从 0 变 1 或者从 1 变 0),从而使得经过翻转操作后的矩阵里有尽可能多的行(横着从左到右)的值是一样的(全是 0 或者全是 1)。假设这样一种情况,如果在经过某次翻转操作后,使得第 i 行都为 0 了,那么我们可以得到如下结论

  • 如果此时第 j 行上所有的值也为 0,说明第 j 行在翻转前跟第 i 行应该是一样的
  • 如果此时有一个第 k 行上所有的值为 1,说明第 k 行在翻转前跟第 i 行应该是恰好相反的

所以这个问题可以转化为找到一个第 i 行使得与这个 row 完全相同的 row 或者完全相反的 row 最多。

具体的做法是我们需要扫描这个二维矩阵,对于当前要扫描的行 row 的所有值我们取反,记为 row2,比如 000110 我们找到 111001,然后去矩阵里找看有多少行是和 row 或者 row2 相同的。

时间O(mn)

空间O(n)

Java实现

 1 class Solution {
 2     public int maxEqualRowsAfterFlips(int[][] matrix) {
 3         int res = 0;
 4         int m = matrix.length;
 5         int n = matrix[0].length;
 6         for (int i = 0; i < m; i++) {
 7             int count = 0;
 8             int[] flip = new int[n];
 9             for (int j = 0; j < n; j++) {
10                 flip[j] = 1 - matrix[i][j];
11             }
12             for (int k = i; k < m; k++) {
13                 if (Arrays.equals(matrix[k], matrix[i]) || Arrays.equals(matrix[k], flip)) {
14                     count++;
15                 }    
16             }
17             res = Math.max(res, count);
18         }
19         return res;
20     }
21 }

 

二刷我参考了另一个思路,我个人觉得这个思路更容易记住。他的具体做法是对于每一行,我们做一下转化,以每行第一个元素为准,记为 T,如果之后的元素跟第一个元素相同,也记为 T,否则记为 F。这样,对于如下的矩阵,

[[0,0,0],
[0,0,1],
[1,1,0]]

他的模式其实是,

[[T,T,T],
[T,T,F],
[T,T,F]]

对于这个例子而言,他最终返回的是 2,因为第二行和第三行可以通过将第三列反转过来变成

[[0,0,1],
[0,0,0],
[1,1,1]]

这样第二行和第三行所有值都相等。

具体的做法是我们需要用一个 hashmap 记录每一行是什么字符串。对于每一行,我们将第一个数字作为标准,记为 T,如果之后的元素跟第一个元素相同,也记为 T,否则记为 F。最后把这个字符串存入 hashmap。遍历 hashmap 的所有 key,找到出现次数最多的那个 key,他背后的出现次数就是我们最终能得到的最大行数。

时间O(mn)

空间O(n)

Java实现

class Solution {
    public int maxEqualRowsAfterFlips(int[][] matrix) {
        HashMap<String, Integer> map = new HashMap<>();
        int m = matrix.length;
        int n = matrix[0].length;
        for (int i = 0; i < m; i++) {
            StringBuilder sb = new StringBuilder();
            int first = matrix[i][0];
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == first) {
                    sb.append('T');
                } else {
                    sb.append('F');
                }
            }
            String s = sb.toString();
            map.put(s, map.getOrDefault(s, 0) + 1);
        }

        int res = 0;
        for (String key : map.keySet()) {
            res = Math.max(res, map.get(key));
        }
        return res;
    }
}

 

LeetCode 题目总结

posted @ 2021-11-21 15:00  CNoodle  阅读(46)  评论(0编辑  收藏  举报