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

Python Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

Java Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    }
}

C++ Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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;
    }
};

Go Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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)
}