Leetcode121. Best Time to Buy and Sell Stock
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if prices==None or len(prices)<2:
return 0
dp = list(range(len(prices)))
dp[0]=0
minValue = prices[0]
ans = 0
for i in range(1,len(prices)):
dp[i]=prices[i]-minValue
minValue = prices[i] if prices[i]<minValue else minValue
ans = dp[i] if dp[i]>ans else ans
return ans
Runtime: 44 ms, faster than 84.53% of Python3 online submissions for Best Time to Buy and Sell Stock.
Memory Usage: 14.8 MB, less than 5.08% of Python3 online submissions for Best Time to Buy and Sell Stock.

浙公网安备 33010602011771号