Description
Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. (We may choose the same index i multiple times.)
Return the largest possible sum of the array after modifying it in this way.
Example 1:
Example 2:
Example 3:
Note:
- 1 <= A.length <= 10000
- 1 <= K <= 10000
- -100 <= A[i] <= 100
分析
题目的意思是:给定数组A,替换其中的数字变成相反数,同一个数字可以替换无限次。
- 这道题需要用到最小堆,每次把最小的数取出来,然后变成相反数,这样得到的数组的求和就是最大的哈。
代码
参考文献
【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)