Share my C++ solution, easy to understand


  • 0
    V
    class Solution {
    public:
        int maxProduct(vector<int>& nums) {
            int n = nums.size();
            if (n == 0)
                return 0;
            
            int max_product = nums[0];
            int cur_max = nums[0];
            int cur_min = nums[0];
            int tmp1;
            int tmp2;
            
            for (int i = 1; i < n; i++)
            {
                tmp1 = cur_max * nums[i];
                tmp2 = cur_min * nums[i];
                
                cur_max = max(tmp1, nums[i]);
                cur_max = max(tmp2, cur_max);
                
                cur_min = min(tmp2, nums[i]);
                cur_min = min(tmp1, cur_min);
                
                max_product = max(max_product, cur_max);
            }
            
            return max_product;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.