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.empty())
return ;
int price = prices[];
int profit=;
for(auto it = prices.begin();it!=prices.end();++it)
{
if(price<*it)
profit += *it - price;
price = *it;
}
return profit;
}
};

和最大子序列思路差不多。这里是前一天和后一天比较,如果前一天价格小于后一天就先买后卖,否则这一天不交易。

最新文章

  1. iOS开发之单元测试
  2. 记录HttpWebRequest辅助类
  3. html基本的内容
  4. 关于Repository模式
  5. webgame设计之功能模块的代理模式
  6. 使用Spring的Property文件存储测试数据 - 添加测试数据
  7. TextView------文字底部或者中间加横线
  8. java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)
  9. 【原】Storm环境搭建
  10. springmvc获得项目根目录(绝对路径)
  11. Crazy-Links
  12. 汽车Vin码识别——&#160;一款二手车行业值得拥有的OCR识别软件
  13. java面向对象浅析
  14. CodeSmith系列(三)——使用CodeSmith生成ASP.NET页面
  15. ZOJ 1002 DFS
  16. git 一些实用的api
  17. Python开发——2.基本数据类型之数字和字符串
  18. Linux下绑定网卡的操作记录
  19. CH 0101 - a^b / CH 0102 - 64位整数乘法 - [快速幂和快速乘]
  20. Ollydbg

热门文章

  1. c# tcp备忘及networkstream.length此流不支持查找解决
  2. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract
  3. 从零开始学习Linux(cp 命令)
  4. WinJs项目介绍
  5. html-制作导航菜单
  6. Linux 学习手记(5):使用Vim文本编辑器
  7. 回车键和button按钮都绑定同一个事件,如何避免按回车的时候button重复点击
  8. android sdk无法更新或者更新缓慢的解决方案
  9. Python连接MySQL数据库
  10. 常用 windows运行命令