java.lang.Object
g1701_1800.s1755_closest_subsequence_sum.Solution

public class Solution extends Object
1755 - Closest Subsequence Sum.<p>Hard</p> <p>You are given an integer array <code>nums</code> and an integer <code>goal</code>.</p> <p>You want to choose a subsequence of <code>nums</code> such that the sum of its elements is the closest possible to <code>goal</code>. That is, if the sum of the subsequence&rsquo;s elements is <code>sum</code>, then you want to <strong>minimize the absolute difference</strong> <code>abs(sum - goal)</code>.</p> <p>Return <em>the <strong>minimum</strong> possible value of</em> <code>abs(sum - goal)</code>.</p> <p>Note that a subsequence of an array is an array formed by removing some elements <strong>(possibly all or none)</strong> of the original array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [5,-7,3,5], goal = 6</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> Choose the whole array as a subsequence, with a sum of 6. This is equal to the goal, so the absolute difference is 0.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [7,-9,15,-2], goal = -5</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> Choose the subsequence [7,-9,-2], with a sum of -4. The absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,3], goal = -7</p> <p><strong>Output:</strong> 7</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 40</code></li> <li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li> <li><code>-10<sup>9</sup> <= goal <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minAbsDifference

      public int minAbsDifference(int[] nums, int goal)