---
# System prepended metadata

title: 0714. Best Time to Buy and Sell Stock with Transaction Fee
tags: [Leetcode, Dynamic Programming, Medium]

---

# 0714. Best Time to Buy and Sell Stock with Transaction Fee
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
## 思路
每一天只有买股票和卖股票两种状态
如果今天处于**买了股票**的状态说明它有可能保持上一天买股票的状态 或者上一天卖了股票今天又买了
如果今天处于**卖了股票**的状态说明它有可能保持上一天卖股票的状态 或者上一天买了股票今天又卖了
## Code
```java=
class Solution {
    public int maxProfit(int[] prices, int fee) {
        int bought = Integer.MIN_VALUE;
        int sold = 0;
        for(int p:prices){
            bought = Math.max(bought, sold-fee-p);
            sold = Math.max(sold, bought+p);
        }
        return sold;
    }
}
```