LeetCode 1005. Maximize Sum Of Array After K Negations

Given an integer array nums and an integer k, modify the array in the following way:

  • choose an index i and replace nums[i] with -nums[i].

You should apply this process exactly k times. You may choose the same index i multiple times.

Return the largest possible sum of the array after modifying it in this way.

Example 1:

Input: nums = [4,2,3], k = 1
Output: 5
Explanation: Choose index 1 and nums becomes [4,-2,3].

Example 2:

Input: nums = [3,-1,0,2], k = 3
Output: 6
Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].

Example 3:

Input: nums = [2,-3,-1,5,-4], k = 2
Output: 13
Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].

给了一个数组和一个k,需要把数组里的任意数字negate k次,求最后数组之和最大。

其实思路挺直观的,先排个序,然后尽可能negate所有负数直到把k用完。如果k没用完就都是正的了,那就重新再排一次序,看看k是奇数还是偶数,如果是偶数就不用管了,如果是奇数就再把变正以后的数组再排个序,最小的取负。

class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                if (k > 0) {
                    nums[i] = -nums[i];
                    k--;
                } else if (k == 0) {
                    return sum(nums);
                }
            }
        }
        if (k % 2 != 0) {
            Arrays.sort(nums);
            nums[0] = -nums[0];
        }
        return sum(nums);
    }

    private int sum(int[] nums) {
        int result = 0;
        for (int i : nums) {
            result += i;
        }
        return result;
    }
}

然后看了大家的解法,有个O(n)不需要排序的方法,采用了counting sort的思想。采用一个count数组记录每个数字出现的次数(因为题目给的范围是-100到100),一共201种可能性。然后也是尽可能把最小的负数变正,再根据k的奇偶性,如果偶数可以直接求和,奇数就把最小的非负数negate。这个方法就是代码写起来比较复杂,而且因为index mapping特别容易出错,我debug了巨久……

class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int[] count = new int[201];
        for (int i = 0; i < nums.length; i++) {
            count[nums[i] + 100]++;
        }
        for (int i = 0; i < 100; i++) { // -100 - -1
            while (k > 0 && count[i] > 0) {
                count[i]--;
                count[200 - i]++;
                k--;
            }
            if (k == 0) {
                return sum(count);
            }
        }
        if (k % 2 != 0) {
            for (int i = 100; i < 200; i++) {
                if (count[i] > 0) {
                    return sum(count) - 2 * (i - 100);
                }
            }
        }
        return sum(count);
    }

    private int sum(int[] count) {
        int result = 0;
        for (int i = 0; i < count.length; i++) {
            if (count[i] != 0) {
                result += (count[i] * (i - 100));
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值