动态规划
class Solution:
def maxProfit(self, prices):
pre = float('inf')
ans = 0
for now in prices:
if now > pre:
ans += now - pre
pre = now
return ans
定义一个变量保存上一步位置最小的数值,来模拟dp
--遍历股票数值
--如果当前数值大于上一次,将股票卖出,盈利为而二者差值
--然后将上一步数值改为当前数值