题目如下:

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.

Example 1:

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

Example 2:

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

Example 3:

Input: [[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.

Note:

  1. 1 <= matrix.length <= 300
  2. 1 <= matrix[i].length <= 300
  3. All matrix[i].length's are equal
  4. matrix[i][j] is 0 or 1

解题思路:把matrix任意一行的的所有元素拼成一个字符串,例如0010110,要把这行变成全是0或者全是1,那么要经过4次或者3次的列变换。变换之后,很显然matrix中字符串为0010110或者1101001的行最后也会变成全为0或者全为1。因此题目就变成了找出matrix中的某一行,使得在整个matrix中和这行相等的行或者相反的行的最多(即0对应1,1对应0的行)。怎么求出最大值的呢?并查集很适合这个场景。

代码如下:

class Solution(object):
def maxEqualRowsAfterFlips(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: int
"""
parent = [i for i in range(len(matrix))] def find(v1):
p1 = parent[v1]
if p1 != v1:
return find(p1)
return p1 def union(v1,v2):
p1 = find(v1)
p2 = find(v2)
if p1 <= p2:
parent[v2] = p1
else: parent[v1] = p2
def toString(l1):
new_l1 = map(lambda x: str(x), l1)
return ''.join(new_l1) row = []
row_inverse = []
for i in range(len(matrix)):
row.append(toString(matrix[i]))
v1_inverse = ''
for k in row[i]:
v1_inverse += '' if k == '' else ''
row_inverse.append(v1_inverse) for i in range(len(matrix)):
v1 = row[i]
v1_inverse = row_inverse[i]
for j in range(i+1,len(matrix)):
v2 = row[j]
if v1 == v2 or v1_inverse == v2:
union(i,j) dic = {}
res = 0
for i in range(len(matrix)):
p = find(i)
dic[p] = dic.setdefault(p,0) + 1
res = max(res,dic[p]) return res

【leetcode】1072. Flip Columns For Maximum Number of Equal Rows的更多相关文章

  1. 翻转-Flip Columns For Maximum Number of Equal Rows

    2020-02-20 11:00:06 问题描述: 问题求解: 翻转题一个常见的思路就是站在结束的状态来反推最初的状态,本题的解题思路就是站在结束的时候的状态来进行反推. 如果在最终的状态i-row是 ...

  2. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

  3. 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...

  4. 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...

  6. 【leetcode】960. Delete Columns to Make Sorted III

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

  7. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  8. 【leetcode】926.Flip String to Monotone Increasing

    题目如下: A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possib ...

  9. 【leetcode】951. Flip Equivalent Binary Trees

    题目如下: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the ...

随机推荐

  1. Deepin 系统安装并配置PHP开发环境

    Deepin是由武汉深之度科技有限公司开发的Linux发行版.Deepin团队基于Qt/C++(用于前端)和Go(用于后端)开发了的全新深度桌面环境(DDE),以及音乐播放器,视频播放器,软件中心等一 ...

  2. Python编程:从入门到实践—变量和简单数据类型

    变量的命名和使用 #!/usr/bin/env python# -*- encoding:utf-8 -*- message ="Hello Python world!"print ...

  3. malloc函数分配内存失败的常见原因

    malloc()函数分配内存失败的常见原因:  1. 内存不足.  2. 在前面的程序中出现了内存的越界访问,导致malloc()分配函数所涉及的一些信息被破坏.下次再使用malloc()函数申请内存 ...

  4. 20191105 《Spring5高级编程》笔记-第16章

    第16章 Web应用程序 16.3 MVC和Spring MVC介绍 16.3.3 Spring MVC WebApplicationContext层次结构 在Spring MVC中,Dispatch ...

  5. python+selenium元素定位之CSS学习01

    参考文档:https://www.w3school.com.cn/cssref/css_selectors.asp 选择器 例子 例子描述 CSS .class .intro 选择 class=&qu ...

  6. python基础-2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange

    1.编码转换 unicode 可以编译成 UTF-U GBK 即 #!/usr/bin/env python # -*- coding:utf-8 -*- a = '测试字符' #默认是utf-8 a ...

  7. 多线程06-Lock

        ; i < ; i++)             {                 c.Increment();                 c.Decrement();      ...

  8. HDFS网络拓扑概念及机架感知(副本节点选择)

    网络拓扑概念 在本地网络中,两个节点被称为“彼此近邻”是什么意思?在海量数据处理中,其主要限制因素是节点之间数据的传输速率——带宽很稀缺.这里将两个节点间的带宽作为距离的衡量标准. 节点距离:两个节点 ...

  9. 打开虚拟机提示 无法获得vmci 驱动程序的版本:句柄无效

    我从另一台电脑复制过来虚拟机,提示如题. 找到  我的虚拟机的  *.vmx文件(如NeoKylin.vmx),其中有 vmci0.present = "TRUE",将TRUE改为 ...

  10. Appium+Python之元素定位和操作

    一.常用识别元素的工具 uiautomatorviewer:Android SDK自带的一个工具,在tools目录下     二.元素定位   1.格式:find_element_by_定位方式(va ...