LeetCode 1755. Closest Subsequence Sum
Brute Force + Binary Search + Meet in the Middle
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 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 40
-107 <= nums[i] <= 107
-109 <= goal <= 109
Solution:
PreviousLeetCode 1754. Largest Merge Of Two StringsNextLeetCode 1760. Minimum Limit of Balls in a Bag
Last updated