java.lang.Object
g1701_1800.s1785_minimum_elements_to_add_to_form_a_given_sum.Solution

public class Solution extends Object
1785 - Minimum Elements to Add to Form a Given Sum.<p>Medium</p> <p>You are given an integer array <code>nums</code> and two integers <code>limit</code> and <code>goal</code>. The array <code>nums</code> has an interesting property that <code>abs(nums[i]) <= limit</code>.</p> <p>Return <em>the minimum number of elements you need to add to make the sum of the array equal to</em> <code>goal</code>. The array must maintain its property that <code>abs(nums[i]) <= limit</code>.</p> <p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x >= 0</code>, and <code>-x</code> otherwise.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,-1,1], limit = 3, goal = -4</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> You can add -2 and -3, then the sum of the array will be 1 - 1 + 1 - 2 - 3 = -4.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,-10,9,1], limit = 100, goal = 0</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= limit <= 10<sup>6</sup></code></li> <li><code>-limit <= nums[i] <= limit</code></li> <li><code>-10<sup>9</sup> <= goal <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minElements

      public int minElements(int[] nums, int limit, int goal)