【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 ...
随机推荐
- codeforces 704B - Ant Man [想法题]
题目链接:http://codeforces.com/problemset/problem/704/B ------------------------------------------------ ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_6_迭代器的实现原理
- http://blog.csdn.net/sdksdk0/article/details/50749326
http://blog.csdn.net/sdksdk0/article/details/50749326
- sql时间类型相关
1.现在时间 NOW() 示例: select * from users where date<now() 2.时间相减 INTERVAL ' DAY 示例: ' DAY 3.timestamp ...
- 应用安全 - 代码审计 - Python
flask客户端session导致敏感信息泄露 flask验证码绕过漏洞 CodeIgniter 2.1.4 session伪造及对象注入漏洞 沙箱逃逸
- new Date() 对象及方法:
在别人的代码中见了两回 new Date().toLocaleString(),查了才知道,toLocaleString()是 根据本地时间格式,把 Date 对象转换为字符串.于是好奇new Dat ...
- Maven 修改jdk版本
Maven 修改jdk版本方法1: <build> <plugins> <plugin> <groupId>org.apache.maven.plugi ...
- Spark-Core RDD的创建
一.RDD创建的3种方式: 1.从集合中创建RDD 2.从外部存储创建RDD 3.从其他RDD转换得到新的RDD 二.从集合中创建RDD 1.使用parallelize函数创建 scala> v ...
- VS2010中解决Qt“Unable to find a Qt build“
转自:http://blog.sina.com.cn/s/blog_687960370101d0eu.html 三种方法: 1.在QT菜单下单击OPTION,然后单击ADD,选择QT安装路径. 2.运 ...
- NameVirtualHost的使用
如果某个ip:port 上只设置了一个虚拟主机,那么NameVirtualHost ip:port 可以不写,因为从这个ip:port的访问不需要做任何选择:如上例中192.168.1.197只对应于 ...