1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @Title: 买卖股票的最佳时机含手续费 (Best Time to Buy and Sell Stock with Transaction Fee)
// @Author: 15816537946@163.com
// @Date: 2020-12-17 22:46:53
// @Runtime: 96 ms
// @Memory: 7.8 MB
func maxProfit(prices []int, fee int) int {
	var empty int
	hold := -1 << 63

	for _, p := range prices {
		temp := empty
		empty = max(empty, hold+p)
		hold = max(hold, temp-p-fee)
	}
	return empty
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}