思路
初始化:买入股票的最小价格为第一天 (minIn),卖出的最大价格maxs为0, 第一天买,第一天卖 ,利润0 dp【0】=0
每次都假设当天卖出,则当天卖出能得到的最大利润是多少
假设:
如果我在第三天卖出的利润为6,dp[2]=6 第四天卖出的利润是5,第三天能卖出6,显然应该在第三天卖才划算,所以此时dp[3]=6
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
# maxs = 0
# for i in range(len(prices)-1):
# ins = prices[i]
# out = max(prices[i+1:])
# if ins>=out:
# continue
# else:
# maxs=max(maxs,out-ins)
# return maxs
dp =[0]*len(prices)
minIn = prices[0]
maxs = 0
dp[0] = 0
for i in range(1,len(prices)):
minIn = min(minIn,prices[i])
dp[i]=max(dp[i-1],prices[i]-minIn)
return dp[-1]
ps: