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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
  Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
  Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
  engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

给定一个元素代表某股票每天价格的数组,可以买卖股票多次,但不能同时有多个交易,买之前要卖出,求最大利润。

如果当前价格比之前价格高,则可前一天买入,今天卖出,把差值累计到利润中,若明日价更高的话,还可以今日买入,明日再抛出。以此类推,遍历完整个数组后即可求得最大利润。

Java:

public class Solution {
public int maxProfit(int[] prices) {
int res = 0;
for (int i = 0; i < prices.length - 1; ++i) {
if (prices[i] < prices[i + 1]) {
res += prices[i + 1] - prices[i];
}
}
return res;
}
}

Python:

class Solution(object):
def maxProfit(self, prices):
return sum(max(prices[i + 1] - prices[i], 0) for i in range(len(prices) - 1)) 

Python:

class Solution:
def maxProfit(self, prices):
profit = 0
for i in xrange(len(prices) - 1):
profit += max(0, prices[i + 1] - prices[i])
return profit

Python:

class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
profit_sum = 0
for i in xrange(1, len(prices)):
if prices[i] > prices[i-1]:
profit_sum += prices[i] - prices[i-1] return profit_sum  

C++:

public class Solution {
public int maxProfit(int[] prices) {
int res = 0;
for (int i = 0; i < prices.length - 1; ++i) {
if (prices[i] < prices[i + 1]) {
res += prices[i + 1] - prices[i];
}
}
return res;
}
}

类似题目:

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

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

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

[LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

All LeetCode Questions List 题目汇总

  

  

[LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II的更多相关文章

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

    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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

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

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

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

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

  5. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

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

  6. 31. 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 price ...

  7. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

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

  8. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  9. LeetCode 122. 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 ...

随机推荐

  1. 《Java设计模式》之代理模式 -Java动态代理(InvocationHandler) -简单实现

    如题 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式可细分为如下, 本文不做多余解释 远程代理 虚拟代理 缓冲代理 保护代理 借鉴文章 ht ...

  2. rocketmq那些事儿之本地调试环境搭建

    上一篇文章中我们已经介绍过rocketmq的集群环境搭建,然而在源码的学习中我们还需要进行本地的调试和问题的定位查找,毕竟还是在本地方便些,今天就说一说如何进行源码的本地调试 下载编译 对于rocke ...

  3. 在Windows10系统下安装Oracle 11g数据库

    在Windows10系统下安装Oracle 11g数据库 https://blog.csdn.net/wei1992_6/article/details/60054727

  4. KMP + BZOJ 4974 [Lydsy1708月赛]字符串大师

    KMP 重点:失配nxtnxtnxt数组 意义:nxt[i]nxt[i]nxt[i]表示在[0,i−1][0,i-1][0,i−1]内最长相同前后缀的长度 图示: 此时nxt[i]=jnxt[i]=j ...

  5. Collections.synchronizedList与CopyOnWriteArrayList比较

    1.单线程方式 2.多线程版本,不安全的 ArrayList 3.多线程版本,线程安全,CopyOnWriteArrayList()方式 4.多线程版本,线程安全,Collections.synchr ...

  6. LeetCode 1215. Stepping Numbers

    原题链接在这里:https://leetcode.com/problems/stepping-numbers/ 题目: A Stepping Number is an integer such tha ...

  7. Django 实现文件下载

    1. 思路: 文件,让用户下载 - a标签+静态文件 - 设置响应头(django如何实现文件下载) 2. a标签实现 <a href="/static/xxx.xlsx"& ...

  8. MongoDB TTL集合与固定集合

    1.固定集合     MongoDB可以创建固定长度的集合,可以设置最大的集合空间或最大的集合数.创建集合的语法如下:     db.createCollection("collection ...

  9. WinDbg常用命令系列---断点操作b*

    ba (Break on Access) ba命令设置处理器断点(通常称为数据断点,不太准确).此断点在访问指定内存时触发. 用户模式下 [~Thread] ba[ID] Access Size [O ...

  10. 洛谷 P1063 能量项链 题解

    P1063 能量项链 题目描述 在\(Mars\)星球上,每个\(Mars\)人都随身佩带着一串能量项链.在项链上有\(N\)颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并 ...