class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型vector 
     * @return int整型
     */
    int maxProduct(vector<int>& nums) {
        // write code here
        int n = nums.size();
        if (n == 0) return 0;

        int max_prod = nums[0], min_prod = nums[0], result = nums[0];
        int prev_max, prev_min;

        for (int i = 1; i < n; ++i) {
            prev_max = max_prod;
            prev_min = min_prod;

            max_prod = max({nums[i], prev_max * nums[i], prev_min * nums[i]});
            min_prod = min({nums[i], prev_max * nums[i], prev_min * nums[i]});

            result = max(result, max_prod);
        }

        return result;
    }
};