算法: 买卖股票并且有手续费714. Best Time to Buy and Sell Stock with Transaction Fee
Posted AI架构师易筋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法: 买卖股票并且有手续费714. Best Time to Buy and Sell Stock with Transaction Fee相关的知识,希望对你有一定的参考价值。
714. Best Time to Buy and Sell Stock with Transaction Fee
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
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.
Example 2:
Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6
Constraints:
- 1 <= prices.length <= 5 * 104
- 1 <= prices[i] < 5 * 104
- 0 <= fee < 5 * 104
动态规划解法:
给定 any day I,其最大利润状态归结为以下两种状态之一:
(1)购买状态:
buy[i]代表了最大的利润,在day i在买状态,因为你采取的最后一个动作是一个买入的动作在day K其中K<=i。并且您有权在 出售day i+1,或者什么也不做。
(2)销售情况:
sell[i]代表最大的利润在day i在卖出状态,因为你采取的最后一个动作是一个卖出动作的day K,在那里K<=i。并且您有权在 购买day i+1,或者什么都不做。
让我们从基本案例开始。
基本情况:
我们可以从购买状态开始,这意味着我们在 购买股票day 0。
buy[0]=-prices[0];
或者我们可以从卖出状态开始,这意味着我们在day 0.
鉴于我们在第 0 天手头没有任何库存,我们将卖出状态设置为 0
sell[0]=0。;
状态转换:
在day i,我们可以买入股票(从之前的卖出状态)或什么都不做(从之前的买入状态): 或者 在,我们可以卖出股票(从之前的买入状态)或保持持有(从之前的卖出状态):
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]);
day i
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
最后:
我们将返回sell[last_day]作为我们的结果,它代表最后一天的最大利润,因为您在最后一天之前的任何一天采取了卖出行动。
我们可以在买入或卖出状态收取交易费。
所以这里来了我们的两个解决方案:
方案1——卖出股票时支付费用:
public int maxProfit(int[] prices, int fee)
if (prices.length <= 1) return 0;
int days = prices.length, buy[] = new int[days], sell[] = new int[days];
buy[0]=-prices[0];
for (int i = 1; i<days; i++)
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]); // keep the same as day i-1, or buy from sell status at day i-1
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i] - fee); // keep the same as day i-1, or sell from buy status at day i-1
return sell[days - 1];
方案2——购买股票时支付费用:
public int maxProfit(int[] prices, int fee)
if (prices.length <= 1) return 0;
int days = prices.length, buy[] = new int[days], sell[] = new int[days];
buy[0]=-prices[0]-fee;
for (int i = 1; i<days; i++)
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i] - fee); // keep the same as day i-1, or buy from sell status at day i-1
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // keep the same as day i-1, or sell from buy status at day i-1
return sell[days - 1];
参考
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108871/2-solutions-2-states-DP-solutions-clear-explanation!
以上是关于算法: 买卖股票并且有手续费714. Best Time to Buy and Sell Stock with Transaction Fee的主要内容,如果未能解决你的问题,请参考以下文章
力扣算法JS LC [714. 买卖股票的最佳时机含手续费] LC [968. 监控二叉树]
算法: 使用交易费买卖股票的最佳时机714. Best Time to Buy and Sell Stock with Transaction Fee