<span>leetcode-1072 Flip Columns For Maximum Number of Equal Rows</span>

Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column.  Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.

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

输入输出实例:

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

  本题可以使用dict,统计最多的相同的行出现的次数(row 和 reverse(row)视为同一行)

class Solution:
    def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
        def reverse_row(arr):
            res = [0] * len(arr)
            for item, values in enumerate(row):
                res[item] = 1 - values
            return res
        dic = {}
        for row in matrix:
            if tuple(row) in dic:
                dic[tuple(row)] += 1
            elif tuple(reverse_row(row)) in dic: 
                dic[tuple(reverse_row(row))] += 1
            else:
                dic[tuple(row)] = 1
        dic = sorted(dic.items(), key = lambda x:x[1], reverse=True)
        return dic[0][1]

 

全部评论

相关推荐

鼗:眼睛打码有点那啥的味道
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务