152. 乘积最大子数组
题目链接:152. 乘积最大子数组
代码如下:
class Solution
{
public:
int maxProduct(vector<int>& nums)
{
int res=nums[0];
vector<int> f(nums.size()+1,0),g(nums.size()+1,0);
f[0]=nums[0],g[0]=nums[0];
for(int i=1;i<nums.size();i++)
{
f[i]=max(nums[i],max(f[i-1]*nums[i],g[i-1]*nums[i]));//乘积最大值
g[i]=min(nums[i],min(f[i-1]*nums[i],g[i-1]*nums[i]));//乘积最小值
res=max(res,f[i]);
}
return res;
}
};
标签:152,乘积,nums,int,res,数组,max
From: https://blog.csdn.net/weixin_45256307/article/details/139893190