Append K Integers With Minimal Sum LeetCode solution in Python

1
# Runtime: 563 ms (Top 89.1%) | Memory: 31.37 MB (Top 44.5%)
2

3

4
class Solution:
5
def minimalKSum(self, nums: List[int], k: int) -> int:
6
ans = k * (k + 1) // 2
7
prev = -inf
8
for x in sorted(nums):
9
if prev < x:
10
if x <= k:
11
k += 1
12
ans += k - x
13
else:
14
break
15
prev = x
16
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0