###### [leetcode]152. Maximum Product Subarray
//由于负数*负数的结果可能比原值还大，所以要记录一个max还要记录一个min
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n=nums.size();
if(n<1)
return 0;
if(n==1)
return nums[0];

int minLast=nums[0];
int maxLast=nums[0];
int minCur;
int maxCur;
int product=nums[0];

for(int i=1;i<n;i++)
{
maxCur=max(nums[i],max(maxLast*nums[i],minLast*nums[i]));
minCur=min(nums[i],min(maxLast*nums[i],minLast*nums[i]));

maxLast=maxCur;
minLast=minCur;

product=max(product,maxCur);
}
return product;

}
};

#### leetcode 152: Maximum Product Subarray

2014-12-30 09:18:02

#### LeetCode 53/152 Maximum Subarray/Maximum Product Subarray---DP **

2015-04-09 16:57:42

#### LeetCode - 152. Maximum Product Subarray - 思路详解 - C++

2017-01-16 19:26:41

#### LeetCode Maximum Product Subarray 解题报告

2014-10-06 12:09:35

#### [LeetCode] Maximum Product Subarray的4种解法

2014-09-26 12:11:40

#### Maximum Product Subarray -- LeetCode

2014-09-25 01:39:13

#### 【LeetCode-面试算法经典-Java实现】【152-Maximum Product Subarray（子数组的最大乘积）】

2015-08-24 07:07:12

#### LeetCode:152. Maximum Product Subarray Java 子数组最大乘积

2016-09-05 13:52:56

#### LeetCode(53)Maximum Subarray

2014-01-29 11:00:36

#### LeetCode53. Maximum Subarray 动态规划和分治法

2016-09-20 23:26:36

## 不良信息举报

[leetcode]152. Maximum Product Subarray