返回介绍

solution / 1700-1799 / 1785.Minimum Elements to Add to Form a Given Sum / README_EN

发布于 2024-06-17 01:03:14 字数 4456 浏览 0 评论 0 收藏 0

1785. Minimum Elements to Add to Form a Given Sum

中文文档

Description

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.

 

Example 1:

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.

Example 2:

Input: nums = [1,-10,9,1], limit = 100, goal = 0
Output: 1

 

Constraints:

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

Solutions

Solution 1: Greedy

First, we calculate the sum of the array elements $s$, and then calculate the difference $d$ between $s$ and $goal$.

The number of elements to be added is the absolute value of $d$ divided by $limit$ and rounded up, that is, $\lceil \frac{|d|}{limit} \rceil$.

Note that in this problem, the data range of array elements is $[-10^6, 10^6]$, the maximum number of elements is $10^5$, the total sum $s$ and the difference $d$ may exceed the range of 32-bit integers, so we need to use 64-bit integers.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.

class Solution:
  def minElements(self, nums: List[int], limit: int, goal: int) -> int:
    d = abs(sum(nums) - goal)
    return (d + limit - 1) // limit
class Solution {
  public int minElements(int[] nums, int limit, int goal) {
    // long s = Arrays.stream(nums).asLongStream().sum();
    long s = 0;
    for (int v : nums) {
      s += v;
    }
    long d = Math.abs(s - goal);
    return (int) ((d + limit - 1) / limit);
  }
}
class Solution {
public:
  int minElements(vector<int>& nums, int limit, int goal) {
    long long s = accumulate(nums.begin(), nums.end(), 0ll);
    long long d = abs(s - goal);
    return (d + limit - 1) / limit;
  }
};
func minElements(nums []int, limit int, goal int) int {
  s := 0
  for _, v := range nums {
    s += v
  }
  d := abs(s - goal)
  return (d + limit - 1) / limit
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function minElements(nums: number[], limit: number, goal: number): number {
  const sum = nums.reduce((r, v) => r + v, 0);
  const diff = Math.abs(goal - sum);
  return Math.floor((diff + limit - 1) / limit);
}
impl Solution {
  pub fn min_elements(nums: Vec<i32>, limit: i32, goal: i32) -> i32 {
    let limit = limit as i64;
    let goal = goal as i64;
    let mut sum = 0;
    for &num in nums.iter() {
      sum += num as i64;
    }
    let diff = (goal - sum).abs();
    ((diff + limit - 1) / limit) as i32
  }
}
int minElements(int* nums, int numsSize, int limit, int goal) {
  long long sum = 0;
  for (int i = 0; i < numsSize; i++) {
    sum += nums[i];
  }
  long long diff = labs(goal - sum);
  return (diff + limit - 1) / limit;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文