《leetCode》: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 complete at most two transactions.

思路一:报超时错误

思路:分而治之,将prices分成两个部分,分别求这两部分的最大值,每个部分只允许存在一次交易 ;时间复杂度为O(n^2);

实现代码如下:


int getMaxProfitOneTransactions(int* prices, int pricesSize){
    if(prices==NULL||pricesSize<1){
        return 0;
    }
    int max=0;
    int buyPrice=prices[0];
    for(int i=0;i<pricesSize;i++){
        if(prices[i]<buyPrice){
            buyPrice=prices[i];
        }
        else{
            int profit=prices[i]-buyPrice;
            max=(max>profit)?max:profit;
        }
    }
    return max; 
}
int maxProfit(int* prices, int pricesSize) {
    if(prices==NULL||pricesSize<1){
        return 0;
    }
    int max=0;
    for(int i=0;i<pricesSize;i++){
        int maxPartOne_Two=getMaxProfitOneTransactions(prices,i)+getMaxProfitOneTransactions(prices+i,pricesSize-i);
        if(maxPartOne_Two>max){
            max=maxPartOne_Two;
        }
    }
    return max;
}

思路二

思路还是上面的思路,只是就空间来换取时间

/*
思路:分而治之,将prices分成两个部分,分别求这两部分的最大值 
解释:
首先,因为能买2次(第一次的卖可以和第二次的买在同一时间),但第二次的买不能在第一次的卖左边。
所以维护2个表,f1和f2,size都和prices一样大。
意义:
f1[i]表示 -- 截止到i下标为止,左边所做交易能够达到最大profit;
f2[i]表示 -- 从i下标开始,右边所做交易能够达到最大profit;
那么,对于f1 + f2,寻求最大即可。
*/

int max(int a,int b){
    return (a>b)?a:b;
}

int maxProfit(int* prices, int pricesSize) {
    if(prices==NULL||pricesSize<1){
        return 0;
    }

    int *f1=(int *)malloc(pricesSize*sizeof(int));
    int *f2=(int *)malloc(pricesSize*sizeof(int)); 
    if(f1==NULL||f2==NULL){
        exit(EXIT_FAILURE);
    }
    //先看第一个交易
    f1[0]=0;
    int buyPriceMin=prices[0];
    for(int i=1;i<pricesSize;i++){
        if(prices[i]<buyPriceMin){
            buyPriceMin=prices[i];
            f1[i]=f1[i-1];//注意
        }
        else{
            int profit=prices[i]-buyPriceMin;
            f1[i]=max(f1[i-1],profit);
        }
    }
    //再看第二个交易,从右到左,寻找交易利润最大 
    f2[pricesSize-1]=0;
    int SellPriceMax=prices[pricesSize-1];
    for(int i=pricesSize-2;i>=0;i--){
        if(SellPriceMax<prices[i]){
            SellPriceMax=prices[i];
            f2[i]=f2[i+1];//注意
        }
        else{
            int profit=SellPriceMax-prices[i];
            f2[i]=max(f2[i+1],profit);
        }
    }
    //在f1+f2中寻找的最大值就是最大利润值
    int maxProfit=0;
    for(int i=0;i<pricesSize;i++){
        maxProfit=max(maxProfit,f1[i]+f2[i]);
    } 
    return maxProfit;
}

上面的精简版本

int max(int a,int b){
    return (a>b)?a:b;
}
int min(int a,int b){
    return (a<b)?a:b;
}
int maxProfit(int* prices, int pricesSize) {
    if(prices==NULL||pricesSize<1){
        return 0;
    }

    int *f1=(int *)malloc(pricesSize*sizeof(int));
    int *f2=(int *)malloc(pricesSize*sizeof(int)); 
    if(f1==NULL||f2==NULL){
        exit(EXIT_FAILURE);
    }
    //先看第一个交易
    f1[0]=0;
    int buyPriceMin=prices[0];
    for(int i=1;i<pricesSize;i++){
        buyPriceMin=min(buyPriceMin,prices[i]);
        f1[i]=max(f1[i-1],prices[i]-buyPriceMin);
        //用上面两行替换到下面被注释的部分
//      if(prices[i]<buyPriceMin){
//          buyPriceMin=prices[i];
//      }
//      else{
//          int profit=prices[i]-buyPriceMin;
//          f1[i]=max(f1[i-1],profit);
//      }
    }
    //再看第二个交易,从右到左,寻找交易利润最大 
    f2[pricesSize-1]=0;
    int SellPriceMax=prices[pricesSize-1];
    for(int i=pricesSize-2;i>=0;i--){
        SellPriceMax=max(SellPriceMax,prices[i]);
        f2[i]=max(f2[i+1],SellPriceMax-prices[i]);
        //用上面两行替换到下面被注释的部分
//      if(SellPriceMax<prices[i]){
//          SellPriceMax=prices[i];
//      }
//      else{
//          int profit=SellPriceMax-prices[i];
//          f2[i]=max(f2[i+1],profit);
//      }
    }
    //在f1+f2中寻找的最大值就是最大利润值
    int maxProfit=0;
    for(int i=0;i<pricesSize;i++){
        maxProfit=max(maxProfit,f1[i]+f2[i]);
    } 
    return maxProfit;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值