1785. Minimum Elements to Add to Form a Given Sum
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/
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.Input: nums = [1,-10,9,1], limit = 100, goal = 0
Output: 1class Solution:
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
return (abs(goal - sum(nums)) + limit - 1) // limit
Last updated