K 次取反后最大化的数组和 (Maximize Sum Of Array After K Negations)

 

思路:

// @Title: K 次取反后最大化的数组和 (Maximize Sum Of Array After K Negations)
// @Author: qisiii
// @Date: 2024-09-22 18:30:43
// @Runtime: 19 ms
// @Memory: 41.2 MB
// @comment: 
// @flag: 
class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        if(k==0){
            int sum=0;
            for(int i:nums){
                sum+=i;
            }
            return sum;
        }
        //每次取反最小的
        Arrays.sort(nums);
        nums[0]=-nums[0];
        return largestSumAfterKNegations(nums,k-1);
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18