Solved at: 2022-07-13
You are given an array
prices[i] is the price of a given stock on the
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return
def maxProfit(self, prices: List[int]) -> int:
if not prices:
minv = prices
profit = 0
for i, price in enumerate(prices):
if price < minv:
minv = price
if profit < prices[i] - minv:
profit = prices[i] - minv
I had to get a hint to keep the minimum value so far.
- Time Complexity:
- Space Complexity:
- 1698 ms, faster than 38.38% of Python3 online submissions for Best Time to Buy and Sell Stock.
- 25 MB, less than 37.97% of Python3 online submissions for Best Time to Buy and Sell Stock.