121. Best Time to Buy and Sell Stock¶
Edited by Charles
Easy · Python · Greedy
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
You are given an array prices where:
is the price of a given stock on day i.
Original study notes follow, including intermediate snippets and earlier attempts.
Problem¶
You are given an array prices where:
is the price of a given stock on day i.
You want to maximize your profit by choosing:
You may complete at most one transaction.
Return the maximum profit.
If no profit is possible, return:
Example¶
The best transaction is:
LeetCode 121 allows only:
That means:
We cannot do:
multiple times.
A tempting idea is to calculate the price difference between every two consecutive days:
Then add all positive differences:
For:
we get:
Therefore:
Adding all positive differences gives:
But this corresponds to:
This requires two transactions.
Therefore, this solves:
rather than LeetCode 121.
For LeetCode 121, we can think about the problem from the perspective of the selling day.
Suppose today we sell the stock.
Then the maximum profit we can make is:
Therefore, while traversing the array, we only need to maintain:
and:
For every price:
assume:
Then:
Update the maximum profit:
Then update the minimum price:
Consider:
Initialize:
Day 1 — Price = 1¶
If we sell today:
So:
Update minimum price:
Current state:
Day 2 — Price = 5¶
If we sell today:
Therefore:
Minimum price remains:
Day 3 — Price = 3¶
If we sell today:
Therefore:
Minimum price:
Day 4 — Price = 6¶
If we sell today:
Therefore:
Day 5 — Price = 4¶
If we sell today:
Therefore:
Final answer:
During the traversal:
This is an important point.
When we are at:
minPrice only contains the minimum price we have seen before or at the current position.
Therefore, when calculating:
the buying day cannot occur after the selling day.
We are always doing:
so the time ordering is automatically satisfied.
For example:
There is no profitable transaction.
During the traversal:
every possible profit is negative.
Because we initialize:
the result remains:
which means:
There is another interesting way to understand LeetCode 121.
Calculate the daily price changes:
For:
we get:
Suppose we buy at price 1 and sell at price 6.
The profit is:
But we can also write it as:
Therefore, one stock transaction corresponds to choosing a continuous segment of the daily profit array.
So LeetCode 121 can also be transformed into:
This is exactly:
or the idea behind:
Because:
allows us to skip negative days.
For:
adding only positive values gives:
But one transaction cannot skip the middle price drop.
If we buy before the +4 and sell after the +3, we must also experience:
Therefore:
This is why the daily changes must form a continuous subarray.
Using the original profits idea, we can write:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profits = [
prices[i] - prices[i - 1]
for i in range(1, len(prices))
]
res = 0
cur = 0
for profit in profits:
cur = max(0, cur + profit)
res = max(res, cur)
return res
Here:
This correctly enforces the one-transaction constraint.
The direct stock-price approach is simpler:
Time Complexity¶
We traverse prices once:
Space Complexity¶
We only maintain:
Therefore:
LeetCode 121 allows:
Therefore:
For every day, assume we sell today:
So we maintain:
The core idea is:
Do not use:
because that represents multiple transactions and belongs to:
A useful connection to remember is:
while:
Study record
| Field | Value |
|---|---|
| Attempts | 5 |
| Mastery | ❚❚❚❚❚ |
| The Latest Attempt | September 18, 2026 |
Python solution¶
class Solution:
def maxProfit(self, prices: List[int]) -> int:
minPrice = prices[0]
res = 0
for price in prices[1:]:
res = max(res, price - minPrice)
minPrice = min(minPrice, price)
return res