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

来源:互联网 发布:好玩的网络手游 编辑:程序博客网 时间:2024/05/21 10:54
class Solution {    public int maxProfit(int[] prices, int fee) {        int n=prices.length;        if(prices==null||n==0) return 0;        int[] buy=new int[n];        int[] sell=new int[n];        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]);    }}

阅读全文
0 0
原创粉丝点击