Skip to content

Commit f7415c8

Browse files
authored
Merge pull request #1850 from hi-rachel/main
[hi-rachel] Week 05 Solutions
2 parents 561b930 + 817a5fe commit f7415c8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

best-time-to-buy-and-sell-stock/hi-rachel.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
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

315
class 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 풀이

0 commit comments

Comments
 (0)