This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1005. Maximize Sum Of Array After K Negations Easy, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are largestSumAfterKNegations.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given 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.)
03
04Return the largest possible sum of the array after modifying it in this way.
05
06
07
08Example 1:
09
10Input: A = [4,2,3], K = 1
11Output: 5
12Explanation: Choose indices (1,) and A becomes [4,-2,3].
13Example 2:
14
15Input: A = [3,-1,0,2], K = 3
16Output: 6
17Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].
18Example 3:
19
20Input: A = [2,-3,-1,5,-4], K = 2
21Output: 13
22Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4].
23
24
25Note:
26
271 <= A.length <= 10000
281 <= K <= 10000
29-100 <= A[i] <= 100
30**/
31
32//Runtime: 20 ms, faster than 10.38% of C++ online submissions for Maximize Sum Of Array After K Negations.
33//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Maximize Sum Of Array After K Negations.
34class Solution {
35public:
36 int largestSumAfterKNegations(vector<int>& A, int K) {
37 for(int i = 0; i < K; i++){
38 //every time reverse the smallest element
39 vector<int>::iterator it = min_element(A.begin(), A.end());
40 *it = -*it;
41 }
42
43 return accumulate(A.begin(), A.end(), 0);
44 }
45};
Cost