maximum product subarray

PHOTO EMBED

Wed Mar 17 2021 01:57:12 GMT+0000 (UTC)

Saved by @tilak #c++

int maxProduct(vector<int>& nums) {
        int32_t res=nums[0];
        
        for(int32_t i=1, min=res, max = res; i < nums.size(); i++)
        {
            if(nums[i] < 0)
                swap(min, max);
            max = std::max(nums[i] * max, nums[i]);
            min = std::min(nums[i] * min, nums[i]);
            res = std::max(res, max);
        }
        return res;
    }
content_copyCOPY