[LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- [转]C++中模板的特化与偏特化
转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...
- android 线程间的通信
(转自:http://www.cnblogs.com/allin/archive/2010/05/19/1738800.html) andriod提供了 Handler 和 Looper 来满足线程间 ...
- YAML语法介绍
官网YAML: http://yaml.org/ http://yaml.org/xml 参考链接: http://www.ruanyifeng.com/blog/2016/07/yaml.html? ...
- Cannot assign to read only property 'exports' of object '#<Object>'
基本原因是import 和module.exports 是不能混用在.vue的文件里面的 webpack 2中不允许混用import和module.exports module.exports={ 改 ...
- Word 2013无法启用Restrict Editing解决方法
当前文档可能是Mail Merge Letter type document,MAILINGS -> Start Mail Merge -> Normal Word Document保存即 ...
- C语言位操作--两整数中的最大值与最小值
不用选择分支找出指定两整数中的最大值与最小值: int x; int y; // 找出x与y的最大值与最小值 int r; // r保存结果 r = y ^ ((x ^ y) & -(x &l ...
- 应用Strong Name保存.NET应用程序集
关于Strong Name的主题,网上已经有很多这方面的介绍,你可能最熟悉的印象就是这样 大部分的情况,这样就可以了.如果代码是机密的,还可能用到Delay sign only,这就复杂一些,请查找相 ...
- 记我的第一个python爬虫
捣鼓了两天,终于完成了一个小小的爬虫代码.现在才发现,曾经以为那么厉害的爬虫,在自己手里实现的时候,也不过如此.但是心里还是很高兴的. 其实一开始我是看的慕课上面的爬虫教学视屏,对着视屏的代码一行行的 ...
- Tornado,bootstrap文档链接
http://demo.pythoner.com/itt2zh/ch1.html#ch1-1 http://www.runoob.com/bootstrap/bootstrap-tutorial.ht ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...