1072. Flip Columns For Maximum Number of Equal Rows
- 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
来源:CSDN
作者:orientliu96
链接:https://blog.csdn.net/Orientliu96/article/details/103616358