题目链接
https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/
代码
class Solution:
def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
nums.sort(key=lambda x: abs(x), reverse=True)
for i in range(len(nums)):
if nums[i] < 0 and k > 0:
nums[i] *= -1
k -=1
if k % 2 == 1:
nums[len(nums) - 1] *= -1
return sum(nums)