讨论/《高频算法实战》 - 练习:乘积最大的子序列/
《高频算法实战》 - 练习:乘积最大的子序列
共 1 个回复

C++

class Solution {
public:
    double maxProductII(vector<double>& nums) {
    int start = 0,curstart = 0,end=0,curend=0;
    double sum = log(nums[0]),cursum = 0;
    for(int i = 0;i<nums.size();i++)
    {
        cursum += log(nums[i]);
        if(cursum>sum){
            sum = cursum;
            start = curstart;
            end = i;
        }
        if(cursum<0){
            cursum = 0;
            curstart = i+1;
            curend = i+1;
        }
    }
    sum = 1;
    for(int i = start;i<=end;i++)
        sum *= nums[i];
    return sum;
    }
};

image.png