File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxProfit (self , prices : List [int ]) -> int :
3+ # Initialize two pointers: left (buy day) and right (sell day)
4+ l , r = 0 , 1
5+
6+ # Variable to store the maximum profit
7+ maxp = 0
8+
9+ # Iterate through the price list
10+ while r < len (prices ):
11+ # If the current price is greater than the buying price, calculate profit
12+ if prices [l ] < prices [r ]:
13+ profit = prices [r ] - prices [l ] # Profit if we buy at prices[l] and sell at prices[r]
14+ maxp = max (maxp , profit ) # Update max profit if current profit is greater
15+ else :
16+ # If the price at r is lower than at l, move the buy pointer to r
17+ l = r
18+
19+ # Move the sell pointer to the next day
20+ r += 1
21+
22+ # Return the maximum profit found
23+ return maxp
You can’t perform that action at this time.
0 commit comments