Best Time to Buy and Sell Stock1/2/3/4

Best Time to Buy and Sell Stock1

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.

要求中间只交换一次。

思路:遍历整个数组,用一个变量记录遍历过程中的元素最小值,然后计算当前值与最小值的差值即利润,每次取较大值作为利润。

    int maxProfit(vector<int>& prices) {
        int res=0,buy=INT_MAX;
        for(int i=0;i<prices.size();i++)
        { buy=min(buy,prices[i]);
         res=max(res,prices[i]-buy);
        }
    return res;
    }

Best Time to Buy and Sell Stock2

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).

允许多次交易,求交易完最大例如。

这个很容易,直接累计求和即可。

    int maxProfit(vector<int>& prices) {
      int res=0;
        if(prices.size()<1)return 0;
    for(int i=0;i<prices.size()-1;i++)
    {
        if(prices[i]<prices[i+1])
            res+=prices[i+1]-prices[i];
        
    }
        return res;
    }


Best Time to Buy and Sell Stock3

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.

该题最多只能交易k=2次。这里需要结合贪心和动态规划。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,

全局最优:

global[i][j]=max(global[i-1][j],local[i][j])

即取当前局部最好的,和过往全局最好的中大的那个(之所以是global[i-1][j]而非global[i-1][j-1]因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)

局部变量:

local[i][j]=max(global[i-1][j-1]+max(diff,0) , local[i-1][j]+diff)=(A,B)

A代表全局到i-1天进行j-1次交易,然后加上今天的交易;B代表取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。

int maxProfit(vector<int>& prices) {
if(prices.size()<1)return 0;
int n=prices.size();
int local[n][3]={0};
int global[n][3]={0};
for(int i=1;i<prices.size();i++){
    int diff=prices[i]-prices[i-1];
     for(int j=1;j<3;j++)
      { local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff);
        global[i][j]=max(global[i-1][j],local[i][j]);
      }

                                           }
return global[n-1][2];
    }

Best Time to Buy and Sell Stock4

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 k transactions.

相比较3,这里K不只是2。类似,需要结合贪心和动态规划。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,

全局最优:

global[i][j]=max(global[i-1][j],local[i][j])

即取当前局部最好的,和过往全局最好的中大的那个(之所以是global[i-1][j]而非global[i-1][j-1]因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)

局部变量:

local[i][j]=max(global[i-1][j-1]+max(diff,0) , local[i-1][j]+diff)=(A,B)

A代表全局到i-1天进行j-1次交易,然后加上今天的交易;B代表取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。当然该题有可能出现:k的值远大于prices的天数,比如k是好几百万,而prices的天数就为若干天的话,上面的DP解法就非常的没有效率,这时问题其实退化到2。

int maxProfit(int k,vector<int>& prices){
if(prices.size()<1)return 0;
if(k>=prices.size())return maxProfit2(prices);
int n=prices.size();
vector< vector<int> > lo(n, vector<int>(k+1,0));
vector< vector<int> > gl(n, vector<int>(k+1,0));
for(int i=1;i<n;i++){
    int diff=prices[i]-prices[i-1];
    for(int j=1;j<=k;j++)
    {lo[i][j]=max(gl[i-1][j-1]+max(diff,0),lo[i-1][j]+diff);
      gl[i][j]=max(gl[i-1][j],lo[i][j]);
    }
                     }
return gl[n-1][k];
}
int maxProfit2(vector<int> & prices){
int res=0,i;
for(i=1;i<prices.size();i++)
    if(prices[i]>prices[i-1])res+=prices[i]-prices[i-1];
return res;
}

Best Time to Buy and Sell Stock with Cooldown

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

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) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
这题可以多次交易,但要求一个冷冻期Cooldown之说,就是如果某天卖了股票,那么第二天不能买股票,有一天的冷冻期。

思路:

需要维护三个一维数组buy, sell。其中buy表示持股,sell表示丢股或者冷冻:

buy[i]表示在第i天,此时的最大收益要么不买入股票即和昨天一致;要么再买股票且昨天是冷冻期,利润是前天卖出股票时候得到的利润减去今天股票的价钱。

sell[i]表示在第i天,此时的最大收益要么不卖即和昨天一致;要么昨天持有股票今天卖出去了,利润是昨天持有股票时的利润再加上今天的价格


我们写出递推式为:

buy[i]=max(buy[i-1],sell[i-2]-pricei)

sell[i]=max(sell[i-1],buy[i-1]+price)

代码实现:

    int maxProfit(vector<int>& prices) {
     if(prices.size()<1)return 0;
    int n=prices.size();
        vector<int> buy(n);
        vector<int> sell(n);
        sell[-1]=0;
        sell[0]=0;
        buy[0]=-prices[0];//第0天卖出了利润为0-prices[0]
     
        for(int i=1;i<n;i++)
        {   sell[i]=max(sell[i-1],buy[i-1]+prices[i]);
            buy[i]=max(buy[i-1],sell[i-2]-prices[i]);
           
        }
    return sell[n-1];
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值