class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
n=len(prices)
if n==0:
return 0
low_price=[i for i in prices]
for i in range(1,n):
if low_price[i]>low_price[i-1]:
low_price[i]=low_price[i-1]
profit=[prices[i]-low_price[i] for i in range(n)]
return max(profit)
Here is a cleaner
code:
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
if not prices: return 0 # prices is empty
maxProfit = 0
minPrice = prices[0]
for currPrice in prices:
minPrice = min(minPrice, currPrice)
maxProfit = max(maxProfit, currPrice - minPrice)
return maxProfit
No comments :
Post a Comment