您的位置:首页 > Web前端

leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee

2017-10-28 14:35 369 查看
class Solution {
public int maxProfit(int[] prices, int fee) {
int n=prices.length;
if(prices==null||n==0) return 0;
int[] buy=new int
;
int[] sell=new int
;
buy[0]=-prices[0];//假设第一天买入
sell[0]=0;
for(int i=1;i<n;i++){
buy[i]=Math.max(buy[i-1],sell[i-1]-prices[i]);
//今天是否买取决于等于昨天的买入的钱或者昨天卖出后的钱
sell[i]=Math.max(sell[i-1],buy[i-1]+prices[i]-fee);
//今天是否卖取决于昨天卖出的钱和卖出后是否盈利
}
return Math.max(buy[n-1],sell[n-1]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode