局部变量使用double是因为测试用例[0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0]
class Solution {
public:
int maxProduct(vector<int>& nums) {
vector<double> maxDP(nums.size()), minDP(nums.size());
double ans = nums[0];
maxDP[0] = nums[0];
minDP[0] = nums[0];
if(nums.size() == 1){
return maxDP[0];
}
for(int i = 1; i < nums.size(); i++){
double temp = nums[i];
maxDP[i] = max({maxDP[i - 1] * nums[i], temp, minDP[i - 1] * nums[i]});
minDP[i] = min({maxDP[i - 1] * nums[i], temp, minDP[i - 1] * nums[i]});
ans = max(ans, maxDP[i]);
}
return int(ans);
}
};