121.The best time for buying and selling stocks
topic:
2251.The number of flowers during the flowering period.md
Thought:
Dynamic planning,Find the least problem Minority problem:FirstiThe maximum profit of heaven = max(Firsti-1The maximum profit of heaven, FirstiHeavenly price - forwardi-1The minimum price of heaven)
Code:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# Minority problem:FirstiThe maximum profit of heaven = max(Firsti-1The maximum profit of heaven, FirstiHeavenly price - forwardi-1The minimum price of heaven)
dp = [0] * len(prices)
min_price = prices[0]
for i in range(1, len(prices)):
dp[i] = max(dp[i - 1], prices[i] - min_price)
min_price = min(min_price, prices[i])
return dp[-1]贡献者
这篇文章有帮助吗?
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0