Very simple solution C++


#1
int Solution::maxProduct(const vector<int> &a) {
    int n = a.size();
    
    vector<int>min_p(n,1);
    vector<int>max_p(n,1);
    int max_fill = a[0];
    min_p[0] = a[0];
    max_p[0] = a[0];
    
    for(int i=1;i<n;i++){
        if(a[i]>0){
            min_p[i] = min(min_p[i-1]*a[i],a[i]);
            max_p[i] = max(max_p[i-1]*a[i],a[i]);
        }
        else{
            min_p[i] = min(max_p[i-1]*a[i],a[i]);
            max_p[i] = max(min_p[i-1]*a[i],a[i]);
        }
        if(max_fill < max_p[i])
            max_fill = max_p[i];
    }
    return max_fill;
}