leetcode-Best Time to Buy and Sell Stock

本文深入探讨了股票买卖策略,包括单次交易、不限次数交易以及限定次数交易的情况。通过具体算法分析,揭示了如何在不同交易限制下最大化利润,提供了实用的买卖时机判断方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

解析:

根据买卖特性,必须先低价买再高价卖。

遍历数组,动态更新最低价及当前价格减去最低价的最大差值,即买卖利润。

复杂度:  

 时间O(N)      空间O(1)

代码:

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length <=1) return 0; 
        int min = prices[0];
        int profit = 0;
        for(int i=1 ; i < prices.length; i++){
           if(prices[i] > min){
               profit = Math.max(profit , prices[i]-min);
           }else{
               min = prices[i];
           }
        }
        return profit;
    }
}


122. Best Time to Buy and Sell Stock II 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However,you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解析:

该题与上一题的区别在于该题可进行多次交易。同样根据买卖特性,必须先低价买再高价卖。

因为没有规定买卖时间间隔,可利用贪心算法,只要明天比今天价格高,就今天买入,明天卖出,即对第i -1天而言,只要第 i天的价格 prices[i] > prices[i-1] ,则累加收益。

复杂度:  

 时间O(N)      空间O(1)

代码:

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length <= 1) return 0;
        int profit = 0;
        for(int i = 1; i < prices.length ; i++){
            if(prices[i] > prices[i-1]) profit += (prices[i]-prices[i-1]);
        }
        return profit;
    }
}


123. Best Time to Buy and Sell Stock III 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may completeat most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解析:

该题与上一题的区别在于该题只能进行2次交易。第一题是只能进行一次交易,而交易两次很容易看成第i个点左右各一次,这样通过双重循环(第i 个结点左右 均用题1 方法遍历一次)可以计算。不过这样的时间复杂度为O(N^2),不是很好!

可以用时间交换空间的方法来进行计算。

从左往右遍历一次,计算出第i个点的最大收益left[i];再从右往左遍历一次,计算出右边往左第i个点的最大收益right[i]

最后通过一个循环来计算总收益(left[i]+right[i])的最大值。

复杂度:  

 时间O(N)      空间O(N)

代码:

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length <= 1) return  0;
        int[] left = new int[prices.length];
        int[] right= new int[prices.length];
        int lmin = prices[0];
        int rmax= prices[prices.length-1];
        int profit=0;
        
        //从左往右扫描的每个结点的最大收益
        for (int i = 1; i < prices.length; i++){
            lmin = Math.min(lmin , prices[i]);
            left[i] = Math.max(prices[i]-lmin, left[i-1]);
        }
        
        //从右往左扫描的每个结点的最大收益
        for(int i =prices.length-2 ; i >=0 ; i--){
            rmax = Math.max(rmax, prices[i]);
            right[i] = Math.max(rmax-prices[i],right[i+1]);
        }
        //计算最大值
        for (int i = 1; i < prices.length; i++){
            profit = Math.max(profit,left[i]+right[i]);
        }
        return profit;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值