LeetCode 1072. Flip Columns For Maximum Number of Equal Rows解题报告(python)

青春壹個敷衍的年華 提交于 2019-12-20 08:43:12

1072. Flip Columns For Maximum Number of Equal Rows

  1. Flip Columns For Maximum Number of Equal Rows python solution

题目描述

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.
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.
在这里插入图片描述

解析

以1行为单位,每一行的每个元素只有两个选择,要么是0要么是1。因为队列的翻转是任意次数的,所以只需要统计每行中出现最多模式是什么,出现最多模式的次数就是最终答案。
这里模式是指,例如110代表模式aab,111和000代表模式aaa。
这里需要进行一个对准工作,将110,001转换为统一模式,此处统一转换为0开头的模式。^代表亦或操作

class Solution:
    def maxEqualRowsAfterFlips(self, A):
        dic={}       
        for row in A:
            pattern=[]
            for i in row:
                num=i^row[0]
                pattern.append(num)
            tup=tuple(pattern)
            if tup in dic:
                dic[tup]+=1
            else:
                dic[tup]=1
        return max(dic.values())

Reference

https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/discuss/303752/Python-1-Line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!