Simplest c ++ code, i think :)


  • 1
    L
    class Solution {
    public:
        int maxProduct(vector<int>& nums) {
            int length = nums.size();
            int a = nums[0], b = nums[0];
            int max_prod = nums[0];
            for(int i = 1; i < length; i++) {
                int ta = a, tb =b;
                a = min(min(nums[i], tb * nums[i]), ta * nums[i]);
                b = max(max(nums[i], tb * nums[i]), ta * nums[i]);
                max_prod = max(b, max_prod);
            }
            return max_prod;
        }
    };

  • 0
    A

    How do you think of it?


Log in to reply
 

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