---
# System prepended metadata

title: LC 121. Best Time to Buy and Sell Stock
tags: [easy, leedcode, DP, Greedy, python, c++]

---

# LC 121. Best Time to Buy and Sell Stock

### [Problem link](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)

###### tags: `leedcode` `python` `c++` `easy` `Greedy` `DP`

You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.

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.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return <code>0</code>.

**Example 1:** 

```
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
```

**Example 2:** 

```
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
```

 **Constraints:** 

- <code>1 <= prices.length <= 10<sup>5</sup></code>
- <code>0 <= prices[i] <= 10<sup>4</sup></code>


## Solution 1 - Greedy
#### Python
```python=
class Solution:
    def maxProfit(self, prices: List[int]) -> int: 
        low = float("inf")
        res = 0

        for idx, price in enumerate(prices):
            low = min(low, price)
            res = max(res, price - low)

        return res
```
#### C++
```cpp=
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int lowest = prices[0];
        int res = 0;
        for (int i = 1; i < prices.size(); i++) {
            if (lowest < prices[i]) {
                res = max(res, prices[i] - lowest);
            } else {
                lowest = prices[i];
            }
        }
        return res;
    }
};
```

## Solution 2 - DP
#### Python
```python=
class Solution:
    def maxProfit(self, prices: List[int]) -> int: 
        n = len(prices)
        dp = [[0, 0] for _ in range(n)]
        dp[0][0] = -prices[0]
        dp[0][1] = 0

        for i in range(1, n):
            dp[i][0] = max(dp[i - 1][0], -prices[i])
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])

        return dp[-1][1]
```

>### Complexity
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(1)             |


## Note
sol2:
dp[i][0]: 持有股票最大金額
dp[i][1]: 不持有股票最大金額

dp[i][0] = max(dp[i-1][0], -prices[i]) = max(dp[i-1][0], 買股票)
dp[i][1] = max(dp[i-1][1], prices[i] + dp[i-1][0]) = max(dp[i-1][1], 賣股票)
