c++ sliding window O(n), faster than 91%
Zeros in a slide window can be move to either left side or right side, which ever is smaller. \n\n\nclass Solution {\npublic:\n    int minMoves(vector<int>& num
Zeros in a slide window can be move to either left side or right side, which ever is smaller. \n\n```\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int k) {\n        int ret = INT_MAX;\n        int l=0, r=0; // left position, right position\n        int m=-1; // first zero belongs to right\n        int c=0; // count of 1;\n        int clf0=0;\n        int crf0=0;\n        \n        int t=0;\n        while(l<nums.size()&& nums[l]==0) {\n            l++;\n        }\n        r=l;\n        while(r<nums.size()&&c<k) {\n            if(nums[r]==1) {\n                c++;\n            } else {\n                t += (k-c);\n                crf0++;\n            }\n            r++;\n        }\n        if(c<k)\n            return -1;\n        m=l+1;\n        while(m<r) {\n            int clf1 = m-l-clf0;\n            int crf1 = k-clf1;\n            if(nums[m]==0) {\n                if(clf1<crf1) {\n                    t += clf1;\n                    t -= crf1;\n                    clf0++;\n                    crf0--;\n                } else \n                    break;\n            }\n            m++;\n        }\n        ret = t;\n        r--;\n        //cout<<l<<","<<r<<","<<m<<","<<t<<endl;\n        //cout<<clf0<<","<<crf0<<endl;\n        \n        while(r<nums.size()) {\n            // try move r to next 1\n            r++;\n            while(r<nums.size()&&nums[r]==0) {\n                crf0++;\n                r++;\n            }\n            if(r==nums.size())\n                break;\n            t += crf0;\n            //cout<<"*"<<l<<","<<r<<","<<m<<","<<t<<endl;\n            \n            // try to move l to next 1\n            l++;\n            t -= clf0;\n            while(l<=r &&nums[l]==0) {\n                if(l<m)\n                    clf0--;\n                else {\n                    crf0--;\n                    t -= k;\n                }\n                l++;\n            }\n            //cout<<"**"<<l<<","<<r<<","<<m<<","<<t<<endl;\n            //cout<<clf0<<","<<crf0<<endl;\n            \n            // try to move 0 from right to left, if closer to left\n            if(m<l) {\n                m = l+1;\n                clf0=0;\n            }\n            while(m<r) {\n                int clf1 = m-l-clf0;\n                int crf1 = k-clf1;\n                if(nums[m]==0) {\n                    if(clf1<crf1) {\n                        t += clf1;\n                        t -= crf1;\n                        clf0++;\n                        crf0--;\n                    } else \n                        break;\n                }\n                m++;\n            }\n            //cout<<l<<","<<r<<","<<m<<","<<t<<endl;\n            \n            ret = min(ret, t);\n        }\n        \n        return ret;\n    }\n};\n```
[]