152. Maximum Product Subarray - cocoder39/coco39_LC GitHub Wiki
I knew both positive and negative local should be preserved, but lost in distinguishing positive and negative local. Here we just preserve the max and min of local.
int maxProduct(vector<int>& nums) {
if(nums.empty()){
return 0;
}
int localmax = nums[0];
int localmin = nums[0];
int res = nums[0];
for (int i = 1; i < nums.size(); i++) {
int a = localmax * nums[i];
int b = localmin * nums[i];
localmax = max(nums[i], max(a, b));
localmin = min(nums[i], min(a, b));
res = max(res, localmax);
}
return res;
}