【leetcode】1072. Flip Columns For Maximum Number of Equal Rows
题目如下:
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 <= matrix.length <= 300
1 <= matrix[i].length <= 300
- All
matrix[i].length
's are equalmatrix[i][j]
is0
or1
解题思路:把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的更多相关文章
- 翻转-Flip Columns For Maximum Number of Equal Rows
2020-02-20 11:00:06 问题描述: 问题求解: 翻转题一个常见的思路就是站在结束的状态来反推最初的状态,本题的解题思路就是站在结束的时候的状态来进行反推. 如果在最终的状态i-row是 ...
- 【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 ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...
- 【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 ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【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 ...
- 【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 ...
随机推荐
- qbzt day6 下午 模拟赛
我太菜了 T2 给定一张有向图,每个点有点权.试找到一条路径,使得该路径上的点权最 大值减去点权最小值最大,问这个差最大是多少. 话说这个题第一个想到的思路是tarjan缩点+拓扑排序来着... ...
- ubuntu安装和使用git
1.apt-get安装 apt-get install git 2.全局配置 git config --global user.name "yourname" git config ...
- 让人失望透顶的 CSDN 博客改版
前言 在 CSDN 写博已经 2 年有余,相比一些大佬,时间不算太长.但工作再忙,我也会保持每月产出,从未间断.每天上线回复评论,勘误内容,参加活动,看看阅读量已经成为一种习惯,可以说是 CSDN 博 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第5节 StringBuilder类_1_StringBuilder的原理
字符串不可变.字符串的缓冲区是可以变的 字符串Sting的底层,被final修饰的不可变的数组 a+b+c最终会产生5个字符串
- 几个模拟OLE事件注册、调用的宏
最近遇到一个要求,将原来的OCX控件,替换成直接的DLL调用. 遇到OLE的事件回调,写了三个宏,用于简化代码 #define OLE_ENVENT_IN_CLASS_ONE(event_name, ...
- 抓取某高校附近共享单车位置,并使用web方式展示过去几天的位置变化
效果如图 使用了高德地图API:https://lbs.amap.com/api/javascript-api/example/marker/massmarks js代码如下: function Ma ...
- oracle 11g 数据库恢复技术 ---01 重做日志
一 redo log Oracle数据库中的三大核心文件分别是数据文件(data file).重做日志(redo log)和控制文件(control file).数据文件保证了数据库的持久性,是保存修 ...
- nvm-windows编译源码 go遇到的问题
异常: Microsoft Windows [Version 10.0.17134.1006] (c) Microsoft Corporation. All rights reserved. C:\U ...
- PCB布线设计-模拟和数字布线的异同(转)
工程领域中的数字设计人员和数字电路板设计专家在不断增加,这反映了行业的发展趋势.尽管对数字设计的重视带来了电子产品的重大发展,但仍然存在,而且还会一直存在一部分与模拟或现实环境接口的电路设计.模拟和数 ...
- linux挂载磁盘以及扩容主分区
新买的服务器,如果系统安装操作不当,可能会由于系统主分区过小,后期安装软件过多就会导致软件无法正常运行的问题,如果不做系统格式化,就需要通过购买新的硬盘来进行挂载和扩容主分区以解决问题.本文主要介绍l ...