题目链接:714. 买卖股票的最佳时机含手续费(中等)
算法原理:
解法:动态规划
Java代码:
/**
* Created with IntelliJ IDEA.
* Description:
* User: 王洋
* Date: 2025-10-20
* Time: 20:22
*/
class Solution {
//714.买卖股票的最佳时机含手续费
//看完算法原理后第二天自己写的,一遍过
//吴小哲跟我写的一样
public int maxProfit(int[] prices, int fee) {
int n=prices.length;
int[] f=new int[n];
int[] g=new int[n];
f[0]=-prices[0];
for(int i=1;i<n;i++){
f[i]=Math.max(f[i-1],g[i-1]-prices[i]);
g[i]=Math.max(g[i-1],f[i-1]+prices[i]-fee);
}
return g[n-1];
}
}

378

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



