C++ O(n) Solution Simple


#1
 vector<int> Solution::flip(string A) {
        int r=-1,flag=0;
        pair<int,int> sum=make_pair(0,0);
        pair<int,int> max=make_pair(0,0);
        for(int i=0;i<A.size();i++){
            if(A[i]=='0') sum.first++,flag=1;
            else sum.first--;
            if(sum.first>max.first) max=sum,r=i;
            if(sum.first<0) sum.first=0,sum.second=i+1;
        }
        vector<int> ans;
        if(flag==0) return ans;
        ans.push_back(max.second+1),ans.push_back(r+1);
        return ans;
    }