2195. Append K Integers With Minimal Sum

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2195

class Solution:
    def minimalKSum(self, nums: List[int], k: int) -> int:
        nums.append(0)
        nums.append(2 * 10**9)
        nums.sort()
        res = 0
        for a, b in pairwise(nums):
            n = min(k, b - a - 1)
            if n <= 0:
                continue
            k -= n
            res += (a + 1 + a + n) * n // 2
            if k == 0:
                break
        return res