class Solution {
public:
int maxProduct(int A[], int n) {
if(n==0)
return 0;
int tempMax= A[0];
int tempMin = A[0];
int resMax = A[0];
for(int i = 1; i < n; ++i)
{
int copyMax = tempMax;
tempMax = max(max(tempMax*A[i], A[i]), tempMin*A[i]);
tempMin = min(min(copyMax*A[i], A[i]), tempMin*A[i]);
resMax = max(tempMax, resMax);
}
return resMax;
}
};
leetcode Maximum Product Subarray
最新推荐文章于 2020-02-02 17:32:16 发布