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. java 反序列化PHP

    由于本人所在开发的项目,前期是由php完成的,这里需要对数据库中php序列化的字符串进行反序列化. 1.引入maven依赖 <!--反序列化 php--> <dependency&g ...

  2. tp5 中 model 的修改器

    修改器可以在数据赋值的时候自动进行转换处理 class User extends Model { public function setNameAttr($value){ return strtolo ...

  3. java程序性能优化

    一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...

  4. for_each(c++11)

    http://www.cplusplus.com/reference/algorithm/for_each/ template<class InputIterator, class Functi ...

  5. tomcat 远程调试

    1.服务端查看cataline.sh 中的描述  cataline jpda start 开启服务端远程调试 远程调试端口JPDA_ADDRESS="8000" 2.本地代码参考  ...

  6. NoSQL与RDBMS的九点区别联系

    原文链接:http://blog.sina.com.cn/s/blog_5373fb0b0101ft8a.html     1 理解ACID与BASE的区别(ACID是关系型数据库强一致性的四个要求, ...

  7. xml in SQL

    几年前,学习html时,顺便把xml也学了哈,知道了xpath和xquery的概念,可都没去落实,下面这篇文章,可以学习学习 http://www.cnblogs.com/huyong/archive ...

  8. C#重启IIS指定网站和指定应用程序池

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. ***HTML +CSS 总结与归纳

    一.首先W3C标准 结构.表现.动作  与  html.css.javascript相对应,它本意是结构表现分离,而且按照html规范编写结构. 标签方面: -所有标签都要小写.关闭.并且合理嵌套,i ...

  10. .Net缓存管理框架CacheManager(转)

    转载地址:http://www.cnblogs.com/JustRun1983/p/CacheManager.html Cache缓存在计算机领域是一个被普遍使用的概念.硬件中CPU有一级缓存,二级缓 ...