--- title: leetcode 927 tags: leetcode, math --- ```c++ // https://leetcode.com/problems/three-equal-parts/ class Solution { public: vector<int> threeEqualParts(vector<int>& A) { int ary[30000] = {0}; int cnt = 0, f1 = -1; for (int i=0; i<A.size(); i++) { if (f1 < 0 && A[i] == 1) f1=i; if (A[i]) ary[cnt++] = i; } if (cnt==0) return {0, 2}; for (int i=0; i<cnt-1; i++) { ary[i] = ary[i+1] - ary[i]; } ary[cnt-1] = A.size() - ary[cnt-1]; if (cnt % 3) return {-1, -1}; int i = 0; int x=f1, y=f1; int tmp = cnt/3 - 1; for (i=0; i<tmp; i++) { x += ary[i]; y += ary[i] + ary[tmp + 1 + i]; if (ary[i] != ary[tmp + 1 + i] || ary[i] != ary[tmp*2 + 2 + i]) break; } cout << i << endl; if (i != tmp) return {-1, -1}; cout << ary[tmp] << " " << ary[cnt-1] <<" " << ary[tmp*2 + 1] << endl; if (ary[tmp] < ary[cnt-1] || ary[tmp * 2 + 1] < ary[cnt-1]) return {-1, -1}; return {x + ary[cnt-1]-1, y + ary[tmp] + ary[cnt-1]}; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up