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).

Have you met this question in a real interview?

Yes
Example

Given an example [2,1,2,0,1], return 2

LeetCode上的原题,请参见我之前的博客Best Time to Buy and Sell Stock II

class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
int res = , n = prices.size();
for (int i = ; i < n - ; ++i) {
if (prices[i] < prices[i + ]) {
res += prices[i + ] - prices[i];
}
}
return res;
}
};

[LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二的更多相关文章

  1. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  9. Leetcode-122 Best Time to Buy and Sell Stock II

    #122  Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...

随机推荐

  1. ubuntu 加载新硬盘或分区

    查看目前挂载情况 df -lh 查看新的空间或硬盘 fdisk -lu 新硬盘分区 fdisk /dev/sda 输入m 根据提示输入n(创建一个分区) 然后输入e(扩展分区) 输入分区数1 然后指定 ...

  2. 【原创】threejs实现一个全景地球

    介绍 本demo实现一个旋转的全景地球,效果如下 技术分析 1.球体 2.球体表面贴图 实现 创建容器 <div id="container"></div> ...

  3. iOS_直播类app_HTTP Live Streaming

    http://www.2cto.com/kf/201606/513980.html https://developer.apple.com/library/ios/technotes/tn2224/_ ...

  4. windows系统 SVN出现 can't open file‘\XXX\txn-current-lock’ 拒绝访问 问题处理

    问题描述:  在新建的svn目录下,提交文件出现 如图错误提示.

  5. 获取youku视频下载链接(wireshark抓包分析)

    随便说两句 前两天写了一个python脚本,试图以分析网页源码的方式得到优酷视频的下载地址,结果只得到视频的纯播放地址,下载纯播放地址得到的文件也无法正常播放视频. 这里共享一下播放地址得到的方法(想 ...

  6. (UWP开发)在ListView中通过向右滑动展开汉堡菜单

    首先在移动APP开发中,手势滑动已经成为一个必备的技能,无论大大小小的APP都需要拥有手势滑动功能.在Android和iOS操作系统的APP中,手势滑动比较普及.然而由于国内有关UWP应用的教程比较少 ...

  7. Quartz任务调度基本使用

    转自:http://www.cnblogs.com/bingoidea/archive/2009/08/05/1539656.html 上一篇:定时器的实现.Java定时器Timer和Quartz介绍 ...

  8. dvd管理系统

    >>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...

  9. python 小程序 比较目录间的差异

    比较目录间的差异: I 只按照名称做了比较,如果目录的文件名称相同,但是内容不同脚本认为为相同文件 II 针对目录下面的目录没有循环比较,只是比较了目录的名称 import sys, os def d ...

  10. DelphiXE10.1自定义控件添加图标方法

    1 在资源文件中加入个24*24的BMP图片,命名为控件的类名(全大写包括T)        2 项目文件中加入对应的 {$R *.dres} 缺省为项目文件同名,自动加入到项目文件(Projrct- ...