题解 | #连续子数组的最大乘积#
连续子数组的最大乘积
https://www.nowcoder.com/practice/fd8c819c07c9493887bfac8549c119f4
考虑负负得正情况
比如 -6 * -4 = 24 -2 * -4 = 8 所以也要取min
#include <iostream> #include<vector> #include<algorithm> using namespace std; const int N = 2 * 100010; int f[N], g[N]; int main() { int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i ++){ cin >> a[i]; f[i] = a[i]; g[i] = a[i]; } int res = a[0]; for(int i = 1; i < n; i ++){ f[i] = max(g[i - 1] * a[i], max(f[i - 1] * a[i], f[i])); g[i] = min(f[i - 1] * a[i], min(g[i - 1] * a[i], g[i])); res = max(res, f[i]); } cout << res; return 0; }