LeetCode买卖股票的最佳时机

LeetCode买卖股票的最佳时机

121 买卖股票的最佳时机Ⅰ

题目描述

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0

示例 1:

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104

代码

class Solution {
    public int maxProfit(int[] prices) {
        int res =0; // 记录最大利润
        int sell = 0; // 记录何时卖股票的指针
        int buy = prices[0]; // 当前买入的价格
        while(sell < prices.length){
            int price = prices[sell]; 
            res = Math.max(price - buy,res); // 如果当前卖股票的收益大于当前利润,则更新利润
            buy = Math.min(price,buy); // 如果当前买入股票的价格小于之前买入股票的价格,则在当前买入
            sell++;
        }
        return res;
    }
}

122 买卖股票的最佳时间Ⅱ

题目描述

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

返回 你能获得的 最大 利润

示例 1:

输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
     总利润为 4 + 3 = 7 。

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
     总利润为 4 。

示例 3:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。

提示:

  • 1 <= prices.length <= 3 * 104
  • 0 <= prices[i] <= 104

代码

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        int buy = prices[0];
        int sell = 0;
        while(sell < prices.length){
            int price  = prices[sell];
            if(price > buy){
                res += price - buy;
            }
            buy = price;
            sell++;
        }
        return res;
    }
}

与Ⅰ的不同之处在于只要卖出价格大于买入价格即可卖出

123 买卖股票的最佳时机Ⅲ

题目描述

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

**注意:**你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:prices = [3,3,5,0,0,3,1,4]
输出:6
解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
     随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。   
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。   
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入:prices = [7,6,4,3,1] 
输出:0 
解释:在这个情况下, 没有交易完成, 所以最大利润为 0。

示例 4:

输入:prices = [1]
输出:0

提示:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 105

代码一(会超时)

可以尝试将数组切分成两份,这样就能将问题转化为买卖股票的最佳时机Ⅰ,只要分别求两个子数组的最大利润并相加即可。

class Solution {
    public int getMax(int[] prices){
        int res = 0;
        int buy = prices[0];
        int sell = 0;
        while(sell < prices.length){
            int price = prices[sell];
            res = Math.max(res,price-buy);
            buy = Math.min(price,buy);
            sell++;
        }
        return res;
    }

    public int maxProfit(int[] prices) {
        int res = 0;
        int tem1=0,tem2 = 0;
        for(int i=1;i<prices.length;i++){
            tem1 = getMax(Arrays.copyOfRange(prices,0,i));
            tem2 = getMax(Arrays.copyOfRange(prices,i-1,prices.length));
            res = Math.max(res,tem1+tem2);
        }
        return res;
    }
}

代码二(dp)

用dp1[i]来记录从第一天开始到第 i 天时能获得的最大利润;

用dp2[i]来记录从最后一天开始到第 i 天时能获得的最大利润。

dp1[i] + dp2[i] 即为买卖两次能获得的利润。

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int[] dp1 = new int[len];
        int[] dp2 = new int[len];
        int buy1 = prices[0];
        int sell2 = prices[len-1];
        for(int i=1;i<len;i++){
            int price = prices[i];
            dp1[i] = Math.max(dp1[i-1],price - buy1);
            buy1 = Math.min(buy1,price); 
        }
        for(int i=len-2;i>=0;i--){
            int price = prices[i];
            dp2[i] = Math.max(dp2[i+1],sell2 - price);
            sell2 = Math.max(sell2,price);
        }
        int res = 0;
        for(int i=0;i<len;i++){
            res = Math.max(res,dp1[i] + dp2[i]);
        }
        return res;
    }
}

188 买卖股票的最佳时机Ⅳ

题目描述

给你一个整数数组 prices 和一个整数 k ,其中 prices[i] 是某支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。也就是说,你最多可以买 k 次,卖 k 次。

**注意:**你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

示例 2:

输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
     随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。

提示:

  • 1 <= k <= 100
  • 1 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000

代码

用一个二维数组dp[i][j]记录买卖 i 次时到第 j 个元素所能获得的最大利润

当第 i (i >= 2)次卖出时的,若买入的时间为buy,则此时的利润为:dp[i-1][buy] + 买卖价格差。

class Solution {
    public int maxProfit(int k, int[] prices) {
        int len = prices.length;
        int[][] dp = new int[k][len];
        for(int i=0;i<k;i++){
            if(i==0){
                int buy = prices[0];
                for(int j=1;j<len;j++){
                    int price = prices[j];
                    dp[i][j] = Math.max(dp[i][j-1],price - buy);
                    buy = Math.min(buy,price);
                }
            }else{  
                for(int j=1;j<len;j++){
                    int buy = 0;
                    int price = prices[j];
                    while(buy<j){
                        dp[i][j] = Math.max(dp[i-1][buy]+price - prices[buy],dp[i][j]);
                        buy++;
                    }
                    dp[i][j] = Math.max(dp[i][j],dp[i][j-1]);
                }
            }

        }
        return dp[k-1][len-1];
    }
}
  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林小果呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值