Similar Problems

Similar Problems not available

Closest Subsequence Sum - Leetcode Solution

Companies:

LeetCode:  Closest Subsequence Sum Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming bit-manipulation array two-pointers  

Problem:

Given an integer array nums, return the sum of the closest subsequence of nums that approaches target. Here, a subsequence is defined as a non-empty sequence of integers in nums. A subsequence is called valid if its sum is not too large or small with respect to the target. More formally, for a subsequence of nums, we define its sum distance to target as abs(sum(subsequence) - target).

For each query, you can arbitrarily select the number of packages in each of the warehouses.

Solution:

This problem can be solved using dynamic programming. Let's define dp[i][j] as the minimum absolute difference between target and the sum of all subsequences of nums that can be made using the elements up to the ith index, such that the sum is equal to j.

Now, for every i and j, we have two choices. We can either include the value nums[i] in our subsequence or exclude it.

If we choose to include nums[i], then the subsequence sum will be j + nums[i]. This means that the minimum absolute difference between target and the sum of all subsequences that can be made using the elements upto the ith index with sum equal to j + nums[i] will be min(abs(j + nums[i] - target), dp[i-1][j]).

On the other hand, if we choose to exclude nums[i], then we will not add nums[i] in our subsequence. This means that the minimum absolute difference between target and the sum of all subsequences that can be made using elements upto the ith index with sum equal to j will be dp[i-1][j].

Therefore, we can calculate dp[i][j] as follows:

dp[i][j] = min(abs(j + nums[i] - target), dp[i-1][j], dp[i-1][j+nums[i]])

Finally, we need to find the minimum value of abs(dp[n-1][j] - target) for all possible values of j. Here, n is the length of the nums array.

We can implement this solution using a 2D table and filling it up using a nested loop. Here is the python code for the same:

def closestSubsequence(nums: List[int], target: int) -> int: n = len(nums) dp = [[0] * 20001 for i in range(n)] dp[0][nums[0] + 10000] = 1 # base case

for i in range(1, n):
    for j in range(-10000, 10001):
        if dp[i-1][j + 10000] > 0:
            dp[i][j + 10000] = 1 # exclude nums[i]
            dp[i][j + nums[i] + 10000] = 1 # include nums[i]
            dp[i][j + 10000] = min(dp[i][j + 10000], dp[i-1][j + 10000])
            dp[i][j + nums[i] + 10000] = min(dp[i][j + nums[i] + 10000], dp[i-1][j + 10000])

ans = float('inf')
for i in range(-10000, 10001):
    if dp[n-1][i + 10000] > 0:
        ans = min(ans, abs(i - target))

return ans

This code will run in O(n * target) time complexity as we are iterating over all the possible subsequence sums up to a maximum value of target.

Time Complexity: O(n * target)

Space Complexity: O(n * target)

Closest Subsequence Sum Solution Code

1