Skip to content

Latest commit

 

History

History
50 lines (41 loc) · 1.47 KB

File metadata and controls

50 lines (41 loc) · 1.47 KB

1785. 构成特定和需要添加的最少元素

给你一个整数数组 nums ,和两个整数 limitgoal 。数组 nums 有一条重要属性:abs(nums[i]) <= limit

返回使数组元素总和等于 goal 所需要向数组中添加的 最少元素数量 ,添加元素 不应改变 数组中 abs(nums[i]) <= limit 这一属性。

注意,如果 x >= 0 ,那么 abs(x) 等于 x ;否则,等于 -x

示例 1:

输入: nums = [1,-1,1], limit = 3, goal = -4
输出: 2
解释: 可以将 -2 和 -3 添加到数组中,数组的元素总和变为 1 - 1 + 1 - 2 - 3 = -4 。

示例 2:

输入: nums = [1,-10,9,1], limit = 100, goal = 0
输出: 1

提示:

  • 1 <= nums.length <= 105
  • 1 <= limit <= 106
  • -limit <= nums[i] <= limit
  • -109 <= goal <= 109

题解 (Ruby)

1. 贪心

# @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

题解 (Rust)

1. 贪心

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
    }
}