You are given an integer array nums
and two integers limit
and goal
. The array nums
has an interesting property that abs(nums[i]) <= limit
.
Return the minimum number of elements you need to add to make the sum of the array equal to goal
. The array must maintain its property that abs(nums[i]) <= limit
.
Note that abs(x)
equals x
if x >= 0
, and -x
otherwise.
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: 1
1 <= nums.length <= 105
1 <= limit <= 106
-limit <= nums[i] <= limit
-109 <= goal <= 109
# @param {Integer[]} nums
# @param {Integer} limit
# @param {Integer} goal
# @return {Integer}
def min_elements(nums, limit, goal)
((nums.sum - goal).abs * 1.0 / limit).ceil
end
impl Solution {
pub fn min_elements(nums: Vec<i32>, limit: i32, goal: i32) -> i32 {
((nums.iter().map(|&x| x as i64).sum::<i64>() - goal as i64).abs() as f64 / limit as f64)
.ceil() as i32
}
}