2195. Append K Integers With Minimal Sum Medium
1/**
2 * [2195] Append K Integers With Minimal Sum
3 *
4 * 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.
5 * Return the sum of the k integers appended to nums.
6 *
7 * Example 1:
8 *
9 * Input: nums = [1,4,25,10,25], k = 2
10 * Output: 5
11 * Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3.
12 * The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
13 * The sum of the two integers appended is 2 + 3 = 5, so we return 5.
14 * Example 2:
15 *
16 * Input: nums = [5,6], k = 6
17 * Output: 25
18 * Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
19 * The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum.
20 * The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
21 *
22 *
23 * Constraints:
24 *
25 * 1 <= nums.length <= 10^5
26 * 1 <= nums[i] <= 10^9
27 * 1 <= k <= 10^8
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/append-k-integers-with-minimal-sum/
33// discuss: https://leetcode.com/problems/append-k-integers-with-minimal-sum/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn minimal_k_sum(nums: Vec<i32>, k: i32) -> i64 {
39
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_2195() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.