LeetCode:Best Time to Buy and Sell Stock II
2015-09-23 11:50
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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again).
思路:每一段递增区间的收益累加。
代码:
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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again).
思路:每一段递增区间的收益累加。
代码:
int maxProfit(vector<int> &prices) { int length = prices.size(); int profit = 0; for(int i = 1;i < length;i++) { if(prices[i] > prices[i-1]) profit = profit + (prices[i] - prices[i-1]); } return profit; }
相关文章推荐
- Twitter Storm安装配置(单机版)笔记
- 【学习笔记】系列三:Linux安装APR
- 【学习笔记】系列四:Linux安装PCRE
- password 密码查询
- silverlight调用webservice跨域
- Linux 定时任务 crontab
- java文件下载,上传,解压方法
- 可复用的,静态链表
- Javascript实现的简单右键菜单类
- Storm单机部署方案2
- 值得推荐的C/C++框架和库
- RPM 命令使用总结
- 【学习笔记】系列二:Linux安装配置apache
- 深入理解PHP原理之函数(Introspecting PHP Function)
- 微信开发时遇到底部遮挡输入框的解决方案
- android scroller的原理分析
- RGB HSV HLS三种色彩模式转换(C语言实现)
- 过拟合
- python运行原理
- strom安装1