【leetcode】1072. Flip Columns For Maximum Number of Equal Rows
题目如下:
Given a
matrixconsisting 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 <= 3001 <= matrix[i].length <= 300- All
matrix[i].length's are equalmatrix[i][j]is0or1
解题思路:把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 ...
随机推荐
- redispy
w wuser@ubuntu:~/redispy$ redis-cli > keys * ) "w" ) "wpy" > set w1 w1valu ...
- The MEAN stack is a modern replacement for the LAMP (Linux, Apache, MySQL, PHP/Python) stack
w https://www.mongodb.com/blog/post/building-your-first-application-mongodb-creating-rest-api-using- ...
- 文件格式-CVS:CVS
ylbtech-文件格式-CVS:CVS 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文 ...
- windows传输文件到linux脚本
安装pscp https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html cmd脚本 @echo off rem 拷贝的文件名称 se ...
- 创建虚拟环境virtualenv的小问题
在创建完虚拟环境后,settings里面虚拟环境的python编译器不是虚拟的,而是全局的,这个时候. 由于创建的虚拟环境的存储地址默认是在c盘. 自定义虚拟环境的存储地址步骤: 第一步:在配置环境变 ...
- border-radius 如何计算
1.使用px: 圆的半径是那个px值大小 用这个圆放到div的角上去切割div 2.使用%: 如果长度和宽度一样 圆的半径是长度乘这个百分比得到的结果 如果长度和宽度不一样 产生的效果是宽高乘以百分数 ...
- Spring MVC 向页面传值-Map、Model和ModelMap https://www.cnblogs.com/caoyc/p/5635878.html
Spring MVC 向页面传值-Map.Model和ModelMap 除了使用ModelAndView方式外.还可以使用Map.Model和ModelMap来向前台页面创造 使用后面3种方式,都是在 ...
- dockerFile 配置puppeteer
## install npm && puppeteer## 必要依赖 libXScrnSaver RUN yum -y install libXScrnSaver ## install ...
- struts2 基础5 OGNL、标签、四大域、默认拦截器说明
OGNL表达式 OGNL:对象导抗图语言 OGNL表达式是一个上下文的概念,上下文Map结构 OGNL表达式需要使用#标注命名空间.访问上下文(Context)中的对象需要使用#符号标注命名空间,如# ...
- Mac018--VisualBox & ubuntu 安装
一.安装虚拟机VMware 参考博客:https://blog.csdn.net/u013142781/article/details/50529030 Step1:下载ubuntu镜像 注:选择Ub ...