原题
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/
思路
动态规划
复杂度
时间:O(n)
空间:O(n)
Python代码
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_so_far = 10**4
profit = 0
for price in prices:
min_so_far = min(min_so_far, price)
profit = max(profit, price - min_so_far)
return profit
Go代码
func maxProfit(prices []int) int {
min_so_far := 10000
profit := 0
for _, price := range prices {
min_so_far = min(min_so_far, price)
profit = max(profit, price-min_so_far)
}
return profit
}
500

被折叠的 条评论
为什么被折叠?



