最多两次股票交易-Best Time to Buy and Sell Stock III
2015-09-22 21:41
309 查看
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.(股票交易,最多两次)
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Design an algorithm to find the maximum profit. You may complete at most two transactions.(股票交易,最多两次)
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size()==0)return 0; int n=prices.size(); vector<int> left(n); vector<int> right(n); int min=prices[0]; int max=prices[n-1]; int res=0; for(int i=1;i<n;i++) { min=min<prices[i]?min:prices[i]; left[i]=left[i-1]>(prices[i]-min)?left[i-1]:(prices[i]-min); } for(int j=n-2;j>=0;j--) { max=max>prices[j]?max:prices[j]; right[j]=right[j+1]>(max-prices[j])?right[j+1]:(max-prices[j]); } for(int i=0;i<n;i++) { res=res>(left[i]+right[i])?res:(left[i]+right[i]); } return res; } };
相关文章推荐
- hadoop2.7.1单机版安装部署
- maven学习总结(七)——eclipse中使用Maven创建Web项目
- ImageView、RadioButton、CheckBox等控件学习
- 关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决
- HDU 1248 寒冰王座
- POJ 3660 Cow Contest 最短路floyd
- NAT介绍,防火墙介绍
- 深度学习Matlab工具箱代码注释——cnntrain.m
- 机器学习笔记 贝叶斯学习(上)
- cocos2d-x学习笔记(五)接入百度联盟广告(Android横幅和插屏广告)
- java中double保留小数点
- Oracle 11g安装过程出现“未找到文件”
- 在恩典中生活
- css的优先级
- 【CSS】CSS HTML 常用命名单词
- Html加载swf 兼容IE8 (含以下)显示
- WKT、SRID、EPSG概念
- 自定义Tree
- 二叉树的中序遍历
- main 函数自身递归调用+感想