📅  最后修改于: 2023-12-03 15:40:15.686000             🧑  作者: Mango
给定一个整数数组 nums 和一个整数 k,请你将该数组中的部分元素替换为任意整数,以使替换后的数组中,距离末尾的等距元素之和相等且最小。
def minSubsequence(nums: List[int], k: int) -> List[int]:
nums.sort(reverse=True)
n = len(nums)
sums = [0] * (n + 1)
for i in range(n):
sums[i + 1] = sums[i] + nums[i]
for i in range(1, n + 1):
if sums[i] > sums[n] - sums[i]:
break
b = nums[i - 1:]
mmin, mmax = min(b), max(b)
counts = [0] * (mmax - mmin + 1)
for x in b:
counts[x - mmin] += 1
res = []
for i in range(k):
if counts[mmax - mmin]:
counts[mmax - mmin] -= 1
else:
counts[mmin - mmin] -= 1
res.append(mmax)
sums[i + 1] -= mmax
sums[n - i] -= mmax
if sums[i] > sums[n - i]:
break
return res