Skip to content

Commit ed2c4c4

Browse files
committed
best-time-to-buy-and-sell-stock solution (py, ts)
1 parent 412bf7b commit ed2c4c4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TC: O(N), SC: O(1)
2+
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
max_profit = 0
6+
min_price = prices[0]
7+
8+
for price in prices:
9+
max_profit = max(price - min_price, max_profit)
10+
min_price = min(price, min_price)
11+
return max_profit
12+
13+
# TS ํ’€์ด
14+
# ๋ฐฐ์—ด ์š”์†Œ(์ˆซ์ž๊ฐ’)์„ ์ง์ ‘ ์ˆœํšŒํ•˜๋ ค๋ฉด for ... of ์‚ฌ์šฉ ํ˜น์€ forEach
15+
# for ... in -> ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์˜ด
16+
17+
# function maxProfit(prices: number[]): number {
18+
# let max_profit: number = 0;
19+
# let min_price: number = prices[0];
20+
21+
# for (let price of prices) {
22+
# max_profit = Math.max(max_profit, price - min_price);
23+
# min_price = Math.min(min_price, price);
24+
# }
25+
# return max_profit;
26+
# };

0 commit comments

Comments
ย (0)