File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 1- # TC: O(N), SC: O(1)
1+ """
2+ https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
3+
4+ You are given an array prices where prices[i] is the price of a given stock on the ith day.
5+
6+ 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.
7+
8+ Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
9+
10+ TC: O(N), SC: O(1)
11+ """
12+
13+ from typing import List
214
315class Solution :
416 def maxProfit (self , prices : List [int ]) -> int :
517 max_profit = 0
618 min_price = prices [0 ]
719
820 for price in prices :
9- max_profit = max (price - min_price , max_profit )
1021 min_price = min (price , min_price )
22+ max_profit = max (price - min_price , max_profit )
23+
1124 return max_profit
1225
1326# TS 풀이
You can’t perform that action at this time.
0 commit comments