c++ o(n) time solution and clean code


  • 0
    D

    This idea is similar with the most voted Java solution. And I just rewrite it with c++ code.

    int maxProduct(vector<int>& nums) {
            int maxProd = INT_MIN, minInc = 1, maxInc = 1;
            for(auto n : nums) {
                int t1 = minInc * n;
                int t2 = maxInc * n;
                maxInc = max(n, max(t1, t2));
                minInc = min(n, min(t1, t2));
                maxProd = max(maxInc, maxProd);
            }
            return maxProd;
        }
    

Log in to reply
 

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