#1785
Minimum Elements to Add to Form a Given Sum
specialist · 990 · lc medium +32 · verified · 45% accepted · 287 likes · top 28%
Description
You are given nums (with abs(nums[i]) <= limit), limit, and goal. Add elements with absolute value at most limit to make the sum equal goal. Return the minimum number of elements to add.
Example 1:
Input: nums = [1,-1,1], limit = 3, goal = -4
Output: 2
Explanation: You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.
Example 2:
Input: nums = [1,-10,9,1], limit = 100, goal = 0
Output: 1
Code
1
2
3