【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. 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 += ‘0‘ if k == ‘1‘ else ‘1‘
            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

原文地址:https://www.cnblogs.com/seyjs/p/11026157.html

时间: 2024-11-07 08:07:23

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

1072. Flip Columns For Maximum Number of Equal Rows

题意: 数组只有0  1 组成,现在flip 任意的column , 所谓的 flip 就是把 0 ->1 or 1- >0,  比如 [1 1 0 1] 变成 [0 0 1 0]. 求经过翻转后 All 0 or All 1 的 row. 这题蛮烧脑的,仔细想想没那么复杂. 先说这个题目的算法:找row里 相同或者 完全相反 的row 的数目. 这题饶了一大圈,本质就是上面这一点,为什么呢? 假如有一row 1 1 1 0 1 ,如果要把这一行all0 or all1, 只能选择 flip

【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 left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip

【leetcode】1043. Partition Array for Maximum Sum

题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given arr

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outpu

【leetcode】 Letter Combinations of a Phone Number(middle)

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A =