今天複習以前的
幹發現這個怎麼加了一個test case==
[0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0]
以前寫的都會overflow
剛剛是直接用python嚕過去了
但看解答C++好像可以直接用double== 點點點
int maxProduct(vector<int>& nums) {
double dp_neg = 1;
double dp_pos = 1;
double ans = -DBL_MAX;
for(auto n : nums) {
double tmp_neg = dp_neg * n;
double tmp_pos = dp_pos * n;
ans = max({ans, (double)n, tmp_neg, tmp_pos});
dp_neg = min({1.0, (double)n, tmp_neg, tmp_pos});
dp_pos = max({1.0, (double)n, tmp_neg, tmp_pos});
}
return (int)ans;
}