乘积最大子序列
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; } };
相关文章推荐
- Drillbench Cemcalc v3.9 1CD(动态固井设计分析软件)/Drillbench Suite v6.1 1CD(钻井作业设计及规划软件)
- Linux文件系统上的特殊权限(SUID、SGID、Sticky)的知识点
- django 1.8 官方文档翻译: 3-5-2 使用Django输出PDF
- 21.备忘录模式
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
- bash环境变量
- 读透一本书(转)
- Android实战简易教程-第五十三枪(通过实现OnScrollListener接口实现上拉加载更多功能)
- Flex与服务器交互之四(使用RemoteObject +BlazeDS+TomCat同Java交互及开发环境配置)
- 1055. The World's Richest (25)
- [置顶] Caffe学习笔记
- 算法导论——矩阵链乘法
- 前向型神经网络之BPNN(附源码)
- Coursera机器学习课程笔记(2) Linear Regression
- C++如何取得int型的最大最小值
- 插入排序(二)
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
- Caffe学习:pycaffe接口配置
- Flex与服务器交互之三(使用WebService组件同服务器WebService通讯)
- 反锯齿渲染技术--SMAA