Largest Sum After k Flips
You must perform exactly k flips on nums. A flip picks any one item and changes its sign (multiplies it by -1); the same item may be flipped more than once, and flipping 0 leaves it 0.
Return the largest sum the list can have after exactly k flips. The largest sum is unique, so no tie rule is needed. nums is never empty; with k = 0 the answer is the sum as given.
Example 1
nums [3,-1,0,2]
k 1
returns 6
One flip: turn -1 into 1, giving 3 + 1 + 0 + 2 = 6.
Example 2
nums [2,-3,-1,5,-4]
k 2
returns 13
Two flips: turn -4 and -3 positive, giving 2 + 3 - 1 + 5 + 4 = 13; the -1 has to stay.
Constraints
1 <= len(nums) <= 100-1000 <= nums[i] <= 10000 <= k <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.