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
2
14
3
15
class Solution :
4
16
def maxProfit (self , prices : List [int ]) -> int :
5
17
max_profit = 0
6
18
min_price = prices [0 ]
7
19
8
20
for price in prices :
9
- max_profit = max (price - min_price , max_profit )
10
21
min_price = min (price , min_price )
22
+ max_profit = max (price - min_price , max_profit )
23
+
11
24
return max_profit
12
25
13
26
# TS 풀이
You can’t perform that action at this time.
0 commit comments