JAVA 50ms with my thinking process
thinking process is chinese:https://www.jianshu.com/p/c8dbaaf87644\n\npublic int[] threeEqualParts(int[] A) {\n        int i = 0, j = A.length-1;\n        Deque
thinking process is chinese:https://www.jianshu.com/p/c8dbaaf87644\n```\npublic int[] threeEqualParts(int[] A) {\n        int i = 0, j = A.length-1;\n        Deque<Integer> pre = new ArrayDeque<>();\n        Deque<Integer> mid = new ArrayDeque<>();\n        Deque<Integer> last = new ArrayDeque<>();\n        if(A[0]!=0) pre.offerLast(1);\n        if(A[j]!=0) last.offerLast(1);\n        for(int k = 1; k < j; k++){\n            if(mid.isEmpty() && A[k] == 0) continue;\n            mid.offerLast(A[k]);\n        } \n        while(i < j){\n            int cp = compare(pre,last);\n            if(cp<0){\n                i++;\n                if(A[i] == 0){\n                    if(!pre.isEmpty()) pre.offerLast(0);\n                } \n                else{\n                    if(mid.isEmpty()) break;\n                    pre.offerLast(mid.pollFirst());\n                    while(!mid.isEmpty() && mid.peekFirst()==0) mid.pollFirst();\n                }\n            }else if(cp>0){\n                j--;\n                if(mid.isEmpty()) break;\n                if(A[j] == 0) mid.pollLast();\n                else moveMidToLast(A,mid,last,j);\n            }else{\n                int cp2 = compare(mid,last);\n                \n                if(cp2 == 0) return new int[]{i,j};\n                if(cp2<0) break;\n                else{\n                    j--;\n                    if(mid.isEmpty()) break;\n                    if(A[j] == 0) mid.pollLast();\n                    else moveMidToLast(A,mid,last,j);\n                }\n            }\n        }\n        return new int[]{-1,-1};\n    }\n    private void moveMidToLast(int[] A,Deque<Integer> mid,Deque<Integer> last,int j){\n        int idx = j+1;\n        while(idx<A.length && A[idx++]==0)\n            last.offerFirst(0);\n        last.offerFirst(mid.pollLast());\n    }\n    \n    private int compare(Deque<Integer> a, Deque<Integer> b){\n        if(a.size() < b.size()) return -1;\n        if(a.size() == b.size()){\n            Iterator<Integer> itr = a.iterator();\n            Iterator<Integer> itr2 = b.iterator();\n            while(itr.hasNext()){\n                int fa = itr.next();\n                int fb = itr2.next();\n                if(fa == fb) continue;\n                return fa-fb;\n            }\n\n            return 0;\n        }\n        return 1;\n    }\n```
[]