【文件属性】:
文件名称:股票收益leetcode-leetcode:leetcode摘要
文件大小:1KB
文件格式:ZIP
更新时间:2021-06-30 12:46:53
系统开源
股票收益leetcode
LeetCode
股票问题
Best
Time
to
Buy
and
Sell
Stock
(Easy)
一次交易,找最大收益
for
i
in
prices:
low
=
min(low,
i)
profit
=
max(profit,
i-low)
Best
Time
to
Buy
and
Sell
Stock
II
(Easy)
可多次交易,找总最大收益
for
i
in
range(1,len(prices):
profit+=prices[i]-prices[i-1]
if
prices[i]-prices[i-1]>0
Best
Time
to
Buy
and
Sell
Stock
III
(Hard)
最多两次交易(同理固定次数的多次),这种方法比较合理且避免了DP(好吧其实我不是很会DP)
for
i
in
prices:
buy1
=
min(buy1,
i)
#找当前最低一次买入价格
prof1
=
max(prof1,
i-buy1)
#找第一次交易最大收益
buy2
=
min(buy2,
i-prof1)
#找用第一次收益购买的股票仍
【文件预览】:
leetcode-master
----README.md(2KB)