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. ORACLE发送带附件邮件的二三事之一

    在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...

  2. Git 常用操作和问题解决

    记录一下自己用git作为项目管理过程中常见的错误以及处理方法 1.git pull 出现问题 git pull出现的问题多为远程分支文件和本地冲突 错误提示:error: Your local cha ...

  3. MVC Code First 自动生成数据库时生成的表名会多了一个s

    如图:我的类文件都是不带s的

  4. 用flex做垂直居中

    <div class="flex-cont flex-centerbox"> <div class="center-cont"> < ...

  5. js 与 jq 的节点添加删除实例

    JavaScript实例:XML DOM节点的添加 <!DOCTYPE html> <html> <head> <meta charset="utf ...

  6. rdlc报表相关

    错误提示:1.尚未指定报表定义的来源 注意ReportPath与ReportEmbeddedResource的区别,前者获取或设置本地报表的本地文件系统路径,设置此属性将导致后者属性值被忽略:后者将获 ...

  7. Delphi控件之---UpDown以及其与TEdit的配合使用(比如限制TEdit只能输入数字,还有Object Inspector之组件属性的介绍)

    最近在开发中使用到了UpDown这个控件,但是因为之前没有使用过,所以很不熟悉,于是就编写了一个简单的demo来学习UpDown以及其结合TEdit的用法. 初步的常用功能的简介 目前(2015.08 ...

  8. VS2015 编译 Qwt6.1.3

    VS2015 + Qt5.7 编译 Qwt6.1.3 1. 将 qwtconfig.pri 中的 C:\Qwt 改为  D:/ProgramFiles/C_Library/Qwt/Qwt , 我的Qw ...

  9. VS2013如何避开安装时IE10的限制

    [VS2013]如何避开VS2013必须要有IE10的限制 VS就会告诉我们目前环境不适合安装VS2013,必须升级IE版本到IE10. 在不安装IE10时的时候,安装办法: 将下面一段文字,储存为. ...

  10. linux内存和虚拟内存的使用