# LWC 55：714. Best Time to Buy and Sell Stock with Transaction Fee

## LWC 55：714. Best Time to Buy and Sell Stock with Transaction Fee

Problem:

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.) Return the maximum profit you can make.

Example 1:

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 Output: 8 Explanation: The maximum profit can be achieved by:

• Buying at prices[0] = 1
• Selling at prices[3] = 8
• Buying at prices[4] = 4
• Selling at prices[5] = 9

The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

0 < prices.length <= 50000.

0 < prices[i] < 50000.

0 <= fee < 50000.

• 前i天的持有股票数最大，记为：hold[i]
• 前i天的股票利益最大，记为：unHold[i]

```     public int maxProfit(int[] prices, int fee) {
int n = prices.length;
int[] hold   = new int[n + 1];
int[] unHold = new int[n + 1];

hold[0] = Integer.MIN_VALUE;

for (int i = 1; i <= n; ++i) {
hold[i]   = Math.max(hold[i - 1], unHold[i - 1] - prices[i - 1] - fee);
unHold[i] = Math.max(unHold[i - 1], hold[i - 1] + prices[i - 1]);
}

return unHold[n];
}```

0 条评论

• ### 挑战程序竞赛系列（5）：2.1广度优先搜索

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### ACM Computer Factory

思路： 最大流，不要把机器当结点了，而是机器的每个输入和输出当结点，因为输出的结点可以进一步当作另外一台机器的输入。所以每台机器拆分成两个结点，i 和 i ...

• ### 学习|Android中JetPack中的几个组件简单使用

Android JetPack是一整套的库，工具和指南。可帮助开发者更轻松地编写优质应用。这些组件可帮助您遵循最佳做法、让您摆脱编写样板代码的工作并简化复杂任务...

• ### ElasticSearch搜索引擎在SpringBoot中的实践

实验环境 ES版本：5.3.0 spring bt版本：1.5.9 首先当然需要安装好elastic search环境，最好再安装上可视化插件 elastics...

• ### 车(ju)一步吃卒

题目： 在一个 8 x 8 的棋盘上，有一个白色车（rook）。也可能有空方块，白色的象（bishop）和黑色的卒（pawn）。它们分别以字符 “R”，“.”...

• ### C# 泛型的简单理解(安全、集合、方法、约束、继承)

泛型允许你在编译时实现类型安全。它们允许你创建一个数据结构而不限于一特定的数据类型。然而，当使用该数据结构时，编译器保证它使用的类型与类型安全是相一致的。泛型提...

• ### Leetcode: Valid Sudoku

题目： Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

• ### 关于 “栈” 的那点破事

用生活中最实际的例子来说，类似于砌砖，加入我们要砌一堵墙，我们肯定是从下往上砌，先拿的砖头就砌在最下边，后拿的砖头就砌到最上边。但是如果监工觉得这堵墙不合适，我...