Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
import sys
res, last = 0, sys.maxint
len_prices = len(prices)
for i in range(len_prices):
profit, last = prices[i] - last if prices[i] > last else 0, prices[i]
res += profit
return res

output

1
2
In [488]: s = Solution(); print s.maxProfit([7,1,5,3,6,4])
7

leet code