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

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值