leetcode122 Best Time to Buy and Sell Stock II
2016-03-05 11:12
381 查看
122. Best Time to Buy and Sell Stock II
Total Accepted:79414 Total Submissions:
190920 Difficulty: Medium
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).
class Solution { public: int maxProfit(vector<int>& prices) { if (prices.size() < 2) return 0; vector<int>::iterator cur = ++prices.begin(); int maxPro = 0, x; for (; cur != prices.end(); ++cur) { x = *cur - *(cur - 1); if (x > 0) maxPro += x; } return maxPro; } };
相关文章推荐
- 套期保值
- windows安装redis缓存使用图文教程
- 大学毕业4年-回顾和总结(1)-钱,金钱观
- 31. Next Permutation
- spark streaming插入hbase
- Dubbo阅读笔记——并发控制和连接控制
- TCP的三次握手和四次挥手
- POJ百炼-2757-最长上升子序列
- U-boot分析与移植(2)----U-boot stage1分析
- kaldi-cuda matrix
- jquery extend 函数详解
- 查看hbase中的中文
- 随机生成30道四则运算
- html,body最顶层元素.
- 查看hbase中的中文
- golang1.5 tar.gz 打包目录,代码分享
- Alcatraz使用之vvDocumenter,代码注释工具
- redis安装
- dubbo 问题汇总
- Odoo 8.0深入浅出开发教程(十) 附录