Compact C++ solution


  • 1
    K
    class Solution {
    public:
        int maxProduct(vector<int>& nums) {
            int pre_DPM = nums[0];
            int pre_DPm = nums[0];
            int result  = nums[0];
            for (auto it = nums.begin() + 1; it != nums.end(); it++) {
                int DPM = max(*it, max(*it * pre_DPM, *it * pre_DPm));
                int DPm = min(*it, min(*it * pre_DPM, *it * pre_DPm));
                result  = max(result, DPM);
                pre_DPM = DPM;
                pre_DPm = DPm;
            }
            return result;
        }
    };

Log in to reply
 

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