Given an integer array nums
, an integer limit
, and an integer goal
, your task is to determine the minimum number of elements you need to add (with each new element's absolute value not exceeding limit
) so that the sum of the array equals goal
.
limit
.nums
.
The goal is to find the minimum number of additions required to make the sum of nums
equal to goal
.
The first step is to realize that the only thing that matters is the difference between the current sum of nums
and the desired goal
. We don't care about the actual values in nums
, only their sum.
The brute-force approach would be to try all possible combinations of numbers (with absolute value ≤ limit
) to reach the difference. However, this is highly inefficient, especially for large differences.
Instead, we can optimize by using the largest possible number (in absolute value) each time. This is like trying to pay off a debt using the largest available denomination of coins.
Thus, the minimum number of elements required is the ceiling of the absolute difference divided by limit
.
nums
.
goal
and the current sum.
diff = abs(goal - sum(nums))
.limit
.
ceil(diff / limit)
.
limit
of the difference.We use the absolute value because the difference could be positive or negative, and we can add either positive or negative numbers to reach the goal.
Example:
nums = [1, -1, 1]
, limit = 3
, goal = -4
nums
is 1 + (-1) + 1 = 1
.abs(-4 - 1) = 5
.3
or -3
.-3
(sum is now 1 + (-3) = -2
).
-3
(sum is now -2 + (-3) = -5
).
-3
and -2
(since 2 ≤ limit
).
ceil(5 / 3) = 2
.
O(limit^k)
where k
is the number of elements added).
O(n)
for calculating the sum of nums
, where n
is the length of nums
.
O(1)
since we only use a few variables.
The optimized solution is highly efficient and suitable for large inputs.
The key insight is that the problem reduces to finding the minimal number of steps to reach a target sum, using additions (or subtractions) bounded by limit
. By always using the largest possible value, we guarantee the fewest steps. This greedy strategy makes the solution both elegant and efficient, requiring only a simple calculation after summing the input array.
import math
class Solution:
def minElements(self, nums, limit, goal):
diff = abs(goal - sum(nums))
return math.ceil(diff / limit)
#include <vector>
#include <cmath>
#include <numeric>
class Solution {
public:
int minElements(std::vector<int>& nums, int limit, int goal) {
long long sum = std::accumulate(nums.begin(), nums.end(), 0LL);
long long diff = std::abs(goal - sum);
return (diff + limit - 1) / limit;
}
};
class Solution {
public int minElements(int[] nums, int limit, int goal) {
long sum = 0;
for (int num : nums) sum += num;
long diff = Math.abs(goal - sum);
return (int)((diff + limit - 1) / limit);
}
}
var minElements = function(nums, limit, goal) {
let sum = nums.reduce((a, b) => a + b, 0);
let diff = Math.abs(goal - sum);
return Math.ceil(diff / limit);
};