File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 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+ # };
You canโt perform that action at this time.
0 commit comments