121. Best Time to Buy and Sell Stock
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 55% Topics: Array, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
max_profit, min_price = 0, float("inf")
for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)
return max_profit
Solution from kamyu104/LeetCode-Solutions · MIT