class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
res = 0
for i in range(len(prices)-1):
if prices[i+1]>prices[i]:
res += prices[i+1] - prices[i]
return res
Sol = Solution()
print Sol.maxProfit([4,5,6,5,6,6,7,4])
0,有点蛋疼的题目
1,题目要求:给定一列表,代表股票在各个时间的价格,怎么进行买卖才能使获利最高?
2,一开始想了很多,包括活动选择等贪心问题,但是其实根本不必考虑这些。只需要股票一涨价,就将其卖掉。如果下一个时间还涨,就买回来再在下个时间卖掉。(因为所有时间股票价格都是透明的,因此不用考虑太多,只需要遍历整个价格表就行了)。