您的位置:首页 > 其它

乘积最大子序列

2015-09-04 21:01 274 查看
找出一个序列中乘积最大的连续子序列(至少包含一个数)。

样例

比如, 序列
[2,3,-2,4]
中乘积最大的子序列为
[2,3]
,其乘积为
6


class Solution {
public:
/**
* @param nums: a vector of integers
* @return: an integer
*/
int maxProduct(vector<int>& nums) {
// write your code here
int n = nums.size();
if (n < 1)
{
return 0;
}

int maxV
;
int minV
;
maxV[0] = nums[0];
minV[0] = nums[0];
int result = nums[0];
for (int i = 1; i < n; i++)
{
int a = nums[i] * maxV[i-1];
int b = nums[i] * minV[i-1];
maxV[i] = max(nums[i], max(a, b));
minV[i] = min(nums[i], min(a, b));
if (maxV[i] > result)
{
result = maxV[i];
}
}

return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: