Leetcode: 1755. Closest Subsequence Sum

Description

You are given an integer array nums and an integer goal.

You want to choose a subsequence of nums such that the sum of its elements is the closest possible to goal. That is, if the sum of the subsequence's elements is sum, then you want to minimize the absolute difference abs(sum - goal).

Return the minimum possible value of abs(sum - goal).

Note that a subsequence of an array is an array formed by removing some elements (possibly all or none) of the original array.

Example

Input: nums = [5,-7,3,5], goal = 6
Output: 0
Explanation: Choose the whole array as a subsequence, with a sum of 6.
This is equal to the goal, so the absolute difference is 0.

Info

1 <= nums.length <= 40
-107 <= nums[i] <= 107
-109 <= goal <= 109

分析

2 ^ 40 的操作是不可接受, (-40* 107, 40 * 107 ) 空间倒是不大,可以尝试(本题是不行的)

方法1:
将 2 ^ 40 的均匀切分个 2 个数组。每个数组中任意组合只要计算 2 ^ 20 (实际存储上要远比这个小,因为 nums[i] 的值比较小)。
后面将 2 个数组排序后, 依次从从 2 个数组找到一个数,或者只有一个数。看下是否能组成 target
class Solution(object):
    def minAbsDifference(self, nums, goal):
        """
        :type nums: List[int]
        :type goal: int
        :rtype: int
        """
        N = len(nums)
        diff = float('inf')
        
        def gen(arr):
            n = len(arr)
            ret = {0: 1}
            for v in arr:
                tmp = {}
                tmp.update(ret)
                for vv in ret:
                    if v + vv not in ret:
                        tmp[v+vv] = 1
                ret = tmp
            return ret.keys() 
        
        if 3 >= N:
            nums1 = gen(nums)
            for v in nums1:
                diff = min(diff, abs(v - goal))
            return diff
            
        nums1 = sorted(gen(nums[:N//2]))
        nums2 = sorted(gen(nums[N//2:]))
        N1, N2 = len(nums1), len(nums2)
        p = 0
      
        for i in range(N2-1, -1, -1):
            v = goal - nums2[i]
            pos = bisect.bisect_left(nums1, v, p, N1)
            
            if pos == N1:
                diff = min(diff, abs(v - nums1[-1]))
                break
            if pos > 0:
                diff = min(diff, abs(v - nums1[pos-1]))
            diff = min(diff, abs(v -nums1[pos]))
            p = pos
        return diff


方法2:
考虑到 nums 的长度最大为 40, 任意 21 数相加组成的空间 可以用通过任意 19 个数相加组成的空间计算获的。
在 40 个数字任意选 20 总共的组合的个数是很大的,所以不可行

总结

性能战胜了 80 %的提交

原文地址:https://www.cnblogs.com/tmortred/p/15256305.html