[C++] O(N) time O(N) space, 40ms, 14 lines, 2 loops, easy understand with explanation
\nclass Solution {\npublic:\n    vector<int> threeEqualParts(vector<int>& A) {\n        vector<int> dp;\n        for(int i = 0 ; i < A.size(); i++) // this loop
```\nclass Solution {\npublic:\n    vector<int> threeEqualParts(vector<int>& A) {\n        vector<int> dp;\n        for(int i = 0 ; i < A.size(); i++) // this loop is used to store the index of all 1s\n            if(A[i]) dp.push_back(i);\n        if(dp.size() % 3) return {-1, -1}; // if the number of 1s cannot be devided perfectly by 3, the input is invalid\n\tif(dp.empty()) return {0,2}; // if the number of 1 is zero, then it is natually valid, return {0, 2}\n        int l1 = 0, l2 = dp.size() / 3, l3 = l2 * 2; //if we want to devide into 3 parts, the distribution pattern of 1s in three parts should be the same\n        for(int i = 1; i < l2; i++ ) {\n            int diff = dp[i] - dp[i-1];\n            if(dp[l2+i] - dp[l2+i-1] != diff || dp[l3+i] - dp[l3+i-1] != diff) //unmatched pattern\n                return {-1, -1};\n\t}\n        int tail0 = A.size() - dp.back(); // calculate how many 0s tail\n        if(dp[l3] - dp[l3-1] < tail0 ||   dp[l2] - dp[l2-1] < tail0) return {-1,-1};// all three parts should tail with the same number of 0s with that in the last part\n        return {dp[l2-1] + tail0 - 1, dp[l3-1] + tail0};\n    }\n};\n```
[]