class Solution {
public int maxProfit(int[] prices, int fee) {
int[][] dp = new int[prices.length + 1][2];
dp[0][0]= -prices[0] - fee;
for(int i = 1; i < prices.length + 1;i++){
int price = prices[i - 1];
dp[i][0] = Math.max(Math.max(dp[i - 1][1] - price - fee,dp[i - 1][0]),- price - fee);
dp[i][1] = Math.max(dp[i - 1][0] + price,dp[i - 1][1]);
}
return dp[prices.length][1];
}
}
714. 买卖股票的最佳时机含手续费
最新推荐文章于 2025-12-05 17:02:52 发布
本文探讨了一种通过动态规划解决股票交易中的最大利润问题,考虑了交易手续费。Solution类中的maxProfit函数提供了在给定价格变动和固定手续费情况下,如何找到最佳买卖时机以最大化收益的方法。
332

被折叠的 条评论
为什么被折叠?



