lc 805. Split Array With Same Average

https://leetcode.com/problems/split-array-with-same-average/

一个数组,能否分成两部分,使之平均值相同。解题过程很愉快呀。

经过几次转换,变成一个比较简单的题目。

问题1:原始数组a,分成两个子数组a1,a2。a1和a2的均值相同,则必须等于数组整体均值ave。就是找有没有非空真子集的均值和ave相同。

问题2:整体数组都减去均值,得到新数组a_new,于是就是找a_new中有没有非空真子集的和为0。先检测如果有0值,直接返回True。

问题3:如果这个数组子集和为0,那么有正有负,他们加和为0。我们把a_new根据正负分成两部分,a_p和a_n。那么就是说,a_p中有非空真子集的和加上a_n中某个非空真子集的和为0。

问题4:a_n中的负数都取绝对值。问题转化为:对于两个集合a_p和a_n,是否能各取一个非空真子集,是两个子集的和相同?能返回True,不能则false。

到了问题4,相信已经很简单了,就算不是简单,也是再也不能转化成更简单的描述方式了,剩下的就是怎么用算法搞定它了。

使用两个set,分别记录a_p和a_n的所有子集的和出现的所有可能就行了。时间空间复杂度都是n^2。题目里n<30,so easy~

a_p_set=set()for n in a_p:    for inset in list(a_p_set):        bb.add(inset + n)    bb.add(n)

然后两个set都扔掉最大的那个和(因为真子集,不能包含所有)。然后看有没有交集,有就True,没有就false。

哈哈哈哈哈哈哈,是不是很简单??!

我一提交结果,啪啪啪打脸,直接wa了。

原来是因为float两个set,数学上求和相同的两堆数字用float表示下,求和并不同。怎么办?牺牲一定精度??万一牺牲的精度正好把本来就不同的数字当作同一个怎么办????

zing~想到了。float是我们在求平均数的时候引入的。那么这个float乘以数组长度必然是一个整数,我们在求均值前,先把数组各个数都乘以数组长度,不就都是整数了嘛!

长代码,a了:

注意有一些边界的坑,比如只有一个数,等等

class Solution:
    def splitArraySameAverage(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        l=len(A)
        if l<=1:
            return False
        A=[a*l for a in A]
        s=sum(A)
        ave=s/l

        for i in A:
            if ave==float(i):
                return True
        a=[i-ave for i in A]
        b=[]
        c=[]
        for i in a:
            if i<0:
                c.append(-i)
            else:
                b.append(i)
        bb=set()
        cc=set()
        for n in b:
            for inset in list(bb):
                bb.add(inset + n)
            bb.add(n)
        for n in c:
            for inset in list(cc):
                cc.add(inset + n)
            cc.add(n)
        sb=sum(b)
        sc=sum(c)
        bb.remove(sb)
        cc.remove(sc)
        if len(bb.intersection(cc))>=1:
            return True
        return False

原文地址:https://www.cnblogs.com/waldenlake/p/10104192.html

时间: 2024-10-31 20:17:10

lc 805. Split Array With Same Average的相关文章

[LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

[LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

HihoCoder 1448 Split Array

1448 : Split Array 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an sorted integer array A and an integer K. Can you split A into several sub-arrays that each sub-array has exactly K continuous increasing integers. For example you can split {1

410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:If n is the length of array, as

leetcode 659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

410. Split Array Largest Sum

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: Given m satisfies the following cons

Leetcode: Split Array Largest Sum

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: Given m satisfies the following cons

[LeetCode] Split Array Largest Sum 分割数组的最大值

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note:Given m satisfies the following const

[LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example