Skip to content

Commit 785e8fd

Browse files
docs: 121
1 parent d9915dc commit 785e8fd

File tree

2 files changed

+13
-54
lines changed

2 files changed

+13
-54
lines changed

explanations/121/en.md

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
# 121. Best Time to Buy and Sell Stock
2-
3-
**Difficulty:** Easy
4-
**Link:** https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
5-
6-
## Problem Description
7-
81
You are given an array `prices` where `prices[i]` is the price of a given stock on the `i^th` day.
92

103
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.
114

125
Return *the maximum profit you can achieve from this transaction*. If you cannot achieve any profit, return `0`.
136

147
**Example 1:**
15-
```
8+
```text
169
Input: prices = [7,1,5,3,6,4]
1710
Output: 5
1811
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
1912
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
2013
```
2114

2215
**Example 2:**
23-
```
16+
```text
2417
Input: prices = [7,6,4,3,1]
2518
Output: 0
2619
Explanation: In this case, no transactions are done and the max profit = 0.
@@ -68,7 +61,7 @@ For each price starting from the second:
6861
**Example walkthrough:**
6962
Let's trace through the first example:
7063

71-
```
64+
```text
7265
prices = [7,1,5,3,6,4]
7366
7467
Initial state:
@@ -104,31 +97,5 @@ Result: Return max_profit = 5
10497

10598
> **Note:** The key insight is that we don't need to try every possible buy-sell combination. By tracking the minimum price seen so far, we can calculate the maximum possible profit at each step in O(1) time.
10699
107-
### Solution
108-
109-
```python
110-
class Solution:
111-
def maxProfit(self, prices: List[int]) -> int:
112-
# Handle edge case
113-
if not prices:
114-
return 0
115-
116-
# Initialize variables
117-
min_price = prices[0]
118-
max_profit = 0
119-
120-
# Iterate through the array starting from the second element
121-
for price in prices[1:]:
122-
# Update minimum price if current price is lower
123-
min_price = min(min_price, price)
124-
125-
# Calculate potential profit and update maximum profit
126-
potential_profit = price - min_price
127-
max_profit = max(max_profit, potential_profit)
128-
129-
# Return the maximum profit
130-
return max_profit
131-
```
132-
133100
**Time Complexity:** O(n) - we visit each element exactly once
134101
**Space Complexity:** O(1) - we only use a constant amount of extra space

solutions/121/01.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
class Solution:
2-
def maxProfit(self, prices: List[int]) -> int:
3-
# Handle edge case
4-
if not prices:
5-
return 0
1+
def maxProfit(prices: List[int]) -> int:
2+
if not prices:
3+
return 0
64

7-
# Initialize variables
8-
min_price = prices[0]
9-
max_profit = 0
5+
min_price = prices[0]
6+
max_profit = 0
107

11-
# Iterate through the array starting from the second element
12-
for price in prices[1:]:
13-
# Update minimum price if current price is lower
14-
min_price = min(min_price, price)
8+
for price in prices[1:]:
9+
min_price = min(min_price, price)
10+
potential_profit = price - min_price
11+
max_profit = max(max_profit, potential_profit)
1512

16-
# Calculate potential profit and update maximum profit
17-
potential_profit = price - min_price
18-
max_profit = max(max_profit, potential_profit)
19-
20-
# Return the maximum profit
21-
return max_profit
13+
return max_profit

0 commit comments

Comments
 (0)