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. 思路为Dynamic Programming, 感觉实际上跟sell and buy在同一天实际上一样的结果? dp[i]表示目前最大的profit
动态方程式: dp[i] = dp[i-1] + max(0, prices[i] - prices[i-1])
init: dp[0] = 0 1. Constraints
1) edge case [] => 0 2. Ideas Dynamic Programming T: O(n) S: O(1) using rolling array 3. Code
class Solution:
def buySellStock2(self, prices):
if not prices: return 0
dp, n = [0]*2, len(prices)
for i in range(1, n):
dp[i%2] = dp[i%2-1] + max(0, prices[i] - prices[i-1])
return dp[(n-1)%2] # n-1因为我们要for loop里面最后的元素

[LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming的更多相关文章

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

  2. [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 ...

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

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

  5. leetcode 122. Best Time to Buy and Sell Stock II ----- java

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

  6. Java [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 ...

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

  8. [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 ...

  9. Java for 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. PHP之Composer类库依赖管理神器

    Composer中文版说明见:https://github.com/kaka987/Composer-zh Composer 是PHP的类包依赖管理工具,用它可以轻松的引用第三方类包,类似于node的 ...

  2. c++ 纯虚析构函数

    ; 这就是一个纯虚析构函数,这种定义是允许的. 一般纯虚函数都不允许有实体,但是因为析构一个类的过程中会把所有的父类全析构了,所以每个类必有一个析构函数. 所以.纯虚析构函数需要提供函数的实现,而一般 ...

  3. 属性特性get/set

    get/set访问器是属性的特性: 特性只有内部才用,因此在javaScript中不能直接访问他们: (1)数据属性——包含一个数据值的位置.这个位置可以读入和写入值. 数据属性有描述其行为的四个特性 ...

  4. css修改select默认样式

    先来看看效果图: css: <style media="screen"> .select_demo, .select_list { width: 400px; heig ...

  5. Gnome排序

    Gnome排序(地精排序),起初由Hamid Sarbazi-Azad 于2000年提出,并被称为stupid排序,后来被Dick Grune描述并命名为“地精排序”,作为一个排序算法,和插入排序类似 ...

  6. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT

    [CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...

  7. [吐槽]我为什么讨厌C++

    最近在改currennt的代码,我擦擦擦,实在是忍不了了 C++最恶心的地方在于指针引用与面向对象混用!!TMD各种不匹配 举例: template <typename TDevice> ...

  8. Xcode - Your development team, "", does not support the Push Notifications capability.

    1.问题描述: 从git上checkout了别人的一个工程文件,选择team时,Xcode显示如下问题 Your development team, "xxx.xxx.xxx", ...

  9. javaweb分页的后端实现

    先上demo图 servlet实现部分: package servlet; import java.io.IOException; import java.util.List; import java ...

  10. 从UE(用户体验)到道家誓学再到李小龙

    公司大Boss经常会给我做技术培训,感觉他什么都知道,也挺喜欢听他的课. 本文记录可能比较天马行空,我的语文比较差,很难把自己想表达的说出来,为此我就是记录一样关键字,可能这样还会更好些 背景是讲用户 ...