#include <algorithm>
using namespace std;
class Solution {
public:
int maxProduct(vector<int>& nums) {
int len = nums.size();
int maxhere = nums.at(0);
int minhere = nums.at(0); // Needed in case lowest number becomes largest
// from negative number multiplication
int maxsofar = nums.at(0);
for (int i = 1; i < len; ++i) {
// If multiplying by negative, the min/max switch.
if (nums.at(i) < 0)
swap(maxhere, minhere);
// Either the contiguous sum is better or the current value will
// be used to restart contiguous check.
maxhere = max(nums.at(i), maxhere*nums.at(i));
minhere = min(nums.at(i), minhere*nums.at(i));
//
maxsofar = max(maxhere, maxsofar);
}
return max(maxsofar, maxhere);
}
};