菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
38
0

Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)

原创
05/13 14:22
阅读数 14351

714. 买卖股票的最佳时机含手续费

给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

注意:

0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.

class Solution {
    //  public int maxProfit(int[] prices, int fee) {
    //     int size = prices.length;

    //     if (size <= 1){
    //         return 0;
    //     }

    //     int[][] dp = new int[size+1][2];
    //     dp[0][0] = 0; dp[0][1]= Integer.MIN_VALUE;
    //dp【】【0】就是手里没有股票反之dp[][1]就是手里有股票
    //     for(int day = 0; day < size; day++){
    //         dp[day+1][0] = dp[day][0];
    //         if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
    //             dp[day+1][0] = Math.max(dp[day+1][0], dp[day][1] + prices[day] - prices[day-1] - fee);
    //         }

    //         dp[day+1][1] = dp[day][0];
    //         if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
    //             dp[day+1][1] = Math.max(dp[day+1][1], dp[day][1] + prices[day] - prices[day-1]);
    //         }
    //     }

    //     return dp[size][0];
    // }
     public int maxProfit(int[] prices, int fee) {
        int length = prices.length;
        int keepMax = -prices[0];
        int notKeepMax = 0;

        for (int i = 1; i < length; i++) {
            int keepMaxYesterday = keepMax;
            keepMax = Math.max(keepMax, notKeepMax - prices[i]);
            notKeepMax = Math.max(notKeepMax, keepMaxYesterday + prices[i] - fee);
        }
        return notKeepMax;
    }
}

714. 买卖股票的最佳时机含手续费

给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

注意:

0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.

class Solution {
    //  public int maxProfit(int[] prices, int fee) {
    //     int size = prices.length;

    //     if (size <= 1){
    //         return 0;
    //     }

    //     int[][] dp = new int[size+1][2];
    //     dp[0][0] = 0; dp[0][1]= Integer.MIN_VALUE;
    //dp【】【0】就是手里没有股票反之dp[][1]就是手里有股票
    //     for(int day = 0; day < size; day++){
    //         dp[day+1][0] = dp[day][0];
    //         if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
    //             dp[day+1][0] = Math.max(dp[day+1][0], dp[day][1] + prices[day] - prices[day-1] - fee);
    //         }

    //         dp[day+1][1] = dp[day][0];
    //         if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
    //             dp[day+1][1] = Math.max(dp[day+1][1], dp[day][1] + prices[day] - prices[day-1]);
    //         }
    //     }

    //     return dp[size][0];
    // }
     public int maxProfit(int[] prices, int fee) {
        int length = prices.length;
        int keepMax = -prices[0];
        int notKeepMax = 0;

        for (int i = 1; i < length; i++) {
            int keepMaxYesterday = keepMax;
            keepMax = Math.max(keepMax, notKeepMax - prices[i]);
            notKeepMax = Math.max(notKeepMax, keepMaxYesterday + prices[i] - fee);
        }
        return notKeepMax;
    }
}

发表评论

0/200
38 点赞
0 评论
收藏