返回介绍

solution / 2100-2199 / 2195.Append K Integers With Minimal Sum / README_EN

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

2195. Append K Integers With Minimal Sum

中文文档

Description

You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum.

Return_ the sum of the_ k _integers appended to_ nums.

 

Example 1:

Input: nums = [1,4,25,10,25], k = 2
Output: 5
Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3.
The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
The sum of the two integers appended is 2 + 3 = 5, so we return 5.

Example 2:

Input: nums = [5,6], k = 6
Output: 25
Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum. 
The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 1 <= k <= 108

Solutions

Solution 1

class Solution:
  def minimalKSum(self, nums: List[int], k: int) -> int:
    nums.append(0)
    nums.append(2 * 10**9)
    nums.sort()
    ans = 0
    for a, b in pairwise(nums):
      n = min(k, b - a - 1)
      if n <= 0:
        continue
      k -= n
      ans += (a + 1 + a + n) * n // 2
      if k == 0:
        break
    return ans
class Solution {
  public long minimalKSum(int[] nums, int k) {
    int[] arr = new int[nums.length + 2];
    arr[arr.length - 1] = (int) 2e9;
    for (int i = 0; i < nums.length; ++i) {
      arr[i + 1] = nums[i];
    }
    Arrays.sort(arr);
    long ans = 0;
    for (int i = 1; i < arr.length; ++i) {
      int a = arr[i - 1], b = arr[i];
      int n = Math.min(k, b - a - 1);
      if (n <= 0) {
        continue;
      }
      k -= n;
      ans += (long) (a + 1 + a + n) * n / 2;
      if (k == 0) {
        break;
      }
    }
    return ans;
  }
}
class Solution {
public:
  long long minimalKSum(vector<int>& nums, int k) {
    nums.push_back(0);
    nums.push_back(2e9);
    sort(nums.begin(), nums.end());
    long long ans = 0;
    for (int i = 1; i < nums.size(); ++i) {
      int a = nums[i - 1], b = nums[i];
      int n = min(k, b - a - 1);
      if (n <= 0) continue;
      k -= n;
      ans += 1ll * (a + 1 + a + n) * n / 2;
      if (k == 0) break;
    }
    return ans;
  }
};
func minimalKSum(nums []int, k int) int64 {
  nums = append(nums, 0, 2e9)
  sort.Ints(nums)
  ans := 0
  for i := 1; i < len(nums); i++ {
    a, b := nums[i-1], nums[i]
    n := min(k, b-a-1)
    if n <= 0 {
      continue
    }
    k -= n
    ans += (a + 1 + a + n) * n / 2
    if k == 0 {
      break
    }
  }
  return int64(ans)
}

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

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

发布评论

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