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. SB Admin 2 学习笔记1

    需要掌握能够搭建起一个 dashboard 的能力, 因为很少有运维开发团队有专职的前端, bootstrap 也要讲个基本法. SB Admin 2, 一个免费的 bootstrap theme, ...

  2. 我的CodeF水A题之路

    Codeforces Round #359 (Div. 2) A. Free Ice Cream 题目链接:http://www.codeforces.com/problemset/problem/6 ...

  3. SerialPort 串口开发

    private SerialPort sPort = new SerialPort(); //串行端口资源 /// <summary> /// 函数功能:打开串口/关闭串口 /// < ...

  4. TO BUY

    // book 人月神话 // hardware 乐视.凯酷一生黑 HHKB 白无刻 Filco 奶酪绿 G600 // Book 重构 改善既有代码的设计 java与模式 人月神话(40周年中文纪念 ...

  5. LeetCode之263. Ugly Number

    ------------------------------------------------------------- 如果一个数的质因子只包括2,3,5,那么这个数n可以表示为:n=2x+3y+ ...

  6. 当攻击者熟读兵法,Camouflage病毒实战演示暗度陈仓之计

    "明修栈道,暗度陈仓"的典故许多人都听说过,该典故出自楚汉争霸时期,刘邦意图进入关中,需要攻下关中咽喉之地--陈仓.韩信献出一计:表面上浩浩荡荡地修复通往陈仓的栈道以迷惑陈仓守将, ...

  7. python 的 集合,字典,元组,列表

    元组 tuple  a = (1,2,3) 元组不能修改 可权嵌套列表  如 (1,2,3,[1,2,3]) 里面的列表可修改  一般不这样用 列表list  a = [1,2,3] 集合set  a ...

  8. [转]IIS6.0迁移至IIS7.0

    原文地址:http://www.splaybow.com/post/iis-6.0-7.0.html 公司的项目需要迁移到IIS7的目标机器中 在此做记录 原来server 2003系统 迁到2008 ...

  9. 第五篇:在SOUI中使用XML布局属性指引(pos, offset, pos2type)

    窗口布局的概念 每一个UI都是由大量的界面元素构成的,在Windows编程,这些界面元素的最小单位通常称之为控件. 布局就是这些控件在主界面上的大小及相对位置. 传统的布局一般使用一个4个绝对坐标来定 ...

  10. Python-内置函数小结

    内建函数,Python内置的函数(build in function),不需要引用其他包,一般成为BIF   abs() 计算绝对值,abs(-10),接收number,返回一个number   ma ...