I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1020. Partition Array Into Three Parts With Equal Sum, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are canThreePartsEqualSum.
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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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, return true if and only if we can partition the array into three non-empty parts with equal sums.
03
04Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
05
06
07
08Example 1:
09
10Input: [0,2,1,-6,6,-7,9,1,2,0,1]
11Output: true
12Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
13Example 2:
14
15Input: [0,2,1,-6,6,7,9,-1,2,0,1]
16Output: false
17Example 3:
18
19Input: [3,3,6,5,-2,2,5,1,-9,4]
20Output: true
21Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
22
23
24Note:
25
263 <= A.length <= 50000
27-10000 <= A[i] <= 10000
28**/
29
30//Runtime: 80 ms, faster than 21.72% of C++ online submissions for Partition Array Into Three Parts With Equal Sum.
31//Memory Usage: 12.5 MB, less than 100.00% of C++ online submissions for Partition Array Into Three Parts With Equal Sum.
32
33class Solution {
34public:
35 bool canThreePartsEqualSum(vector<int>& A) {
36 int sum = accumulate(A.begin(), A.end(), 0);
37 int firstSum = 0;
38
39 for(int s1 = 0; s1 < A.size()-1; s1++){
40 firstSum += A[s1];
41 if(firstSum*3 == sum){
42 int secondSum = 0;
43 for(int s2 = s1+1; s2 < A.size(); s2++){
44 secondSum += A[s2];
45 if(secondSum == firstSum) return true;
46 }
47 }
48 }
49
50 return false;
51 }
52};
Cost