class Solution
{
public:
    int maxProduct(vector<int> &nums)
    {
        int n = nums.size(), res = nums[0];
        vector<int> f(n + 1, 0), g(n + 1, 0);
        f[0] = nums[0], g[0] = nums[0]; // 初始化
        for (int i = 1; i < n; i++)
        {
            f[i] = max(nums[i], max(f[i - 1] * nums[i], g[i - 1] * nums[i])); //乘积最大值
            g[i] = min(nums[i], min(g[i - 1] * nums[i], f[i - 1] * nums[i])); //乘积最小值
            res = max(res, f[i]);
        }
        return res;
    }
};
