上午简单学了Data Visualization,下午就在码相关的blog,还没来的及刷新的题,还是先总结下昨天的题吧
买卖股票的最佳时机II
给定一个数组 prices
,其中 prices[i]
表示股票第 i
天的价格。
在每一天,你可能会决定购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以购买它,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
提示:
1 <= prices.length <= 3 * 10^4
0 <= prices[i] <= 10^4
Python 解答如下:
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
return sum([prices[i+1]-prices[i] for i in range(len(prices)-1)if prices[i+1]-prices[i] > 0])
没啥好说的,从头到尾走一遍就行,目前还没想出更好办法。