leetcode_122. Best Time to Buy and Sell Stock II 多次买卖股票,求交易的最大利润
2016-11-16 11:18
537 查看
题目:
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).
题意:
给定数组prices,数组元素i代表股票第i天的价格。假设可以多次买卖股票,问最大收益是多少。(在卖股票之前必须先买回股票)
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n < 2 :
return 0
else :
res = 0 #存储收益
i = 0
while i < n-1 : #遍历每一天的价格
if prices[i+1] > prices[i] : #如果是上升的,则加到res中,最后res即为所有上升子序列的和
res += prices[i+1] - prices[i]
i += 1
return res
笔记:
题目说明可以多次买卖,但是同一时间只能有一股在手里。
这样就可以在每次上升子序列之前买入,在上升子序列结束的时候卖出。相当于能够获得所有的上升子序列的收益。
参考:http://blog.csdn.net/doc_sgl/article/details/11908197
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).
题意:
给定数组prices,数组元素i代表股票第i天的价格。假设可以多次买卖股票,问最大收益是多少。(在卖股票之前必须先买回股票)
代码:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n < 2 :
return 0
else :
res = 0 #存储收益
i = 0
while i < n-1 : #遍历每一天的价格
if prices[i+1] > prices[i] : #如果是上升的,则加到res中,最后res即为所有上升子序列的和
res += prices[i+1] - prices[i]
i += 1
return res
笔记:
题目说明可以多次买卖,但是同一时间只能有一股在手里。
这样就可以在每次上升子序列之前买入,在上升子序列结束的时候卖出。相当于能够获得所有的上升子序列的收益。
参考:http://blog.csdn.net/doc_sgl/article/details/11908197
相关文章推荐
- 股票买卖之多次交易最大利润
- 每天一道LeetCode-----买卖商品问题,计算最大利润,分别有一次交易,两次交易,多次交易的情况
- LeetCode - 股票交易的最大利润
- (LeetCode)Best Time to Buy and Sell Stock -- 求股票买卖的最大利润
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益
- LeetCode 121. Best Time to Buy and Sell Stock--股票买入后再卖出,求最大收益,最多交易一次
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益
- 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。
- [LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)
- LeetCode-122:Best Time to Buy and Sell Stock II (多次股票交易最大利润)
- Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益
- 说你有一个数组,其中第i个元素是第i天给定股票的价格。设计一个算法来找到最大的利润,最多可以完成两个交易。
- [LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- [leetcode 121] Best Time to Buy and Sell Stock---只能买卖股票一次的最大收益
- Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益