Skip to content

Latest commit

 

History

History
194 lines (165 loc) · 4.7 KB

File metadata and controls

194 lines (165 loc) · 4.7 KB

中文文档

Description

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].

 

Constraints:

  • 1 <= nums.length <= 104
  • -100 <= nums[i] <= 100
  • 1 <= k <= 104

Solutions

Python3

class Solution:
    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
        counter = Counter(nums)
        ans = sum(nums)
        for i in range(-100, 0):
            if counter[i]:
                ops = min(counter[i], k)
                ans -= i * ops * 2
                counter[i] -= ops
                counter[-i] += ops
                k -= ops
                if k == 0:
                    break
        if k > 0 and k % 2 == 1 and not counter[0]:
            for i in range(1, 101):
                if counter[i]:
                    ans -= 2 * i
                    break
        return ans

Java

class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int ans = 0;
        Map<Integer, Integer> counter = new HashMap<>();
        for (int num : nums) {
            ans += num;
            counter.put(num, counter.getOrDefault(num, 0) + 1);
        }
        for (int i = -100; i < 0; ++i) {
            if (counter.getOrDefault(i, 0) > 0) {
                int ops = Math.min(counter.get(i), k);
                ans -= (i * ops * 2);
                counter.put(i, counter.getOrDefault(i, 0) - ops);
                counter.put(-i, counter.getOrDefault(-i, 0) + ops);
                k -= ops;
                if (k == 0) {
                    break;
                }
            }
        }
        if (k > 0 && (k % 2) == 1 && counter.get(0) == null) {
            for (int i = 1; i < 101; ++i) {
                if (counter.getOrDefault(i, 0) > 0) {
                    ans -= 2 * i;
                    break;
                }
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int largestSumAfterKNegations(vector<int>& nums, int k) {
        unordered_map<int, int> counter;
        for (int num : nums) ++counter[num];
        int ans = accumulate(nums.begin(), nums.end(), 0);
        for (int i = -100; i < 0; ++i) {
            if (counter[i]) {
                int ops = min(counter[i], k);
                ans -= (i * ops * 2);
                counter[i] -= ops;
                counter[-i] += ops;
                k -= ops;
                if (k == 0) break;
            }
        }
        if (k > 0 && k % 2 == 1 && !counter[0]) {
            for (int i = 1; i < 101; ++i) {
                if (counter[i]) {
                    ans -= 2 * i;
                    break;
                }
            }
        }
        return ans;
    }
};

Go

func largestSumAfterKNegations(nums []int, k int) int {
	ans := 0
	counter := make(map[int]int)
	for _, num := range nums {
		ans += num
		counter[num]++
	}
	for i := -100; i < 0; i++ {
		if counter[i] > 0 {
			ops := min(counter[i], k)
			ans -= (i * ops * 2)
			counter[i] -= ops
			counter[-i] += ops
			k -= ops
			if k == 0 {
				break
			}
		}
	}
	if k > 0 && k%2 == 1 && counter[0] == 0 {
		for i := 1; i < 101; i++ {
			if counter[i] > 0 {
				ans -= 2 * i
				break
			}
		}
	}
	return ans
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

...