Skip to content

Commit af82b73

Browse files
authored
Create 121.py
1 parent b80de9a commit af82b73

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

121 Leetcode/121.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

0 commit comments

Comments
 (0)