# Leetcode 714 - Best time to buy and sell stock with fee

`Note`

`DP`

- Very similar to other stock questions, the only mistake I made was initialize
`dp[9][1]`

`dp[0][1]`

should be`0`

instead of`-fee`

.

- Very similar to other stock questions, the only mistake I made was initialize
`Greedy`

- Looking for
`minPrice`

to buy and`highPrice`

to sell, considering fee. - Use
`minPrice`

to store min price we’ve met. - Be careful of examples like
`[1, 4, 10], fee 2`

. Certainly, we should buy 1 and sell 4. We paid fee while selling it. But actually the optimal trade is to buy 1 and sell 10. So, we should make`minPrice = 4 - 2`

so we won’t calculate fees twice when buying 4 and selling 10.

- Looking for

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

**Example**

1 | Input: prices = [1,3,2,8,4,9], fee = 2 |

`DP`

1 | /** |

`Greedy`

1 | /** |