Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution {
public:
int maxProduct(vector<int>& nums)
{
int n=nums.size();
if(n == 0)
return 0;
if(n == 1)
return nums[0];
int maxlocal = nums[0];
int minlocal = nums[0];
int global = nums[0];
for(int i = 1; i < n; i++)
{
int maxcopy = maxlocal;
maxlocal = max(max(maxlocal*nums[i],nums[i]), minlocal*nums[i]);
minlocal = min(min(maxcopy*nums[i], nums[i]), minlocal*nums[i]);
global = max(maxlocal, global);
}
return global;
}
};
public:
int maxProduct(vector<int>& nums)
{
int n=nums.size();
if(n == 0)
return 0;
if(n == 1)
return nums[0];
int maxlocal = nums[0];
int minlocal = nums[0];
int global = nums[0];
for(int i = 1; i < n; i++)
{
int maxcopy = maxlocal;
maxlocal = max(max(maxlocal*nums[i],nums[i]), minlocal*nums[i]);
minlocal = min(min(maxcopy*nums[i], nums[i]), minlocal*nums[i]);
global = max(maxlocal, global);
}
return global;
}
};