Best Time to Buy and Sell Stock

本文探讨了不同限制条件下股票买卖的最佳策略,包括仅能进行一次交易、多次交易、最多两次交易及限定次数内的交易等场景,并提供了相应的算法实现。


本题有一共有四中问题

1.

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

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.

从前向后,同时记录最小交易价格,到第i天的最大利润

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        
        int pricelength=prices.size();
        if(pricelength==0)
        return 0;
        //vector<int> profit(pricelength-1);
        int minprice=prices[0];
        int maxprofit=0;
        for(int i=1; i<pricelength; i++)
        {
            if(prices[i]<minprice)
            minprice=prices[i];
            else
            {
                if((prices[i]-minprice)>maxprofit)
                {
                    maxprofit=(prices[i]-minprice);
                }
            }
        }
        return maxprofit;
    }
};

2.

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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

解析:每天的价格看作一个曲线,每个上升段的增加值累加,即每个极小值到极大值的增量累加,实现时相邻两天只要增加就对ans进行累加

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        int ret=0;
        for(int i=1; i<n; i++)
        {
           ret+=max(prices[i]-prices[i-1],0);
        }
        return ret;
    }
};

3.

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

解析:本题是第四题的一种情况,结合第四题,参考   点击打开链接      找到动态转移方程 

       在这里定义global[i][j]为前i天交易j次的最大收益,local[i][j]表示前i天交易j次的最大收益,且第i天进行了第j次交易

     diff=price[i]-price[i-1],

      状态转移方程为local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff));

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

对于本题j最大为2

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        
       
        if (prices.empty()||prices.size()<2)
        return 0;
         int day=prices.size();
        vector<vector<int>>global(prices.size()+1,vector<int>(4,0));
        vector<vector<int>>local(prices.size()+1,vector<int>(4,0));
        for (int i=1; i<=day; i++)
        {
            int diff=prices[i-1]-prices[i-2];
            if (i==1) diff=0;
            
            for (int j=1; j<3; j++)
            {
                local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff));
                global[i][j]=max(local[i][j],global[i-1][j]);
            }
        }
        return global[day][2];

    }
};

4.

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

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

解析:本题参考第2题,但有两个问题需要注意

      a. 由于3中时间复杂度为O(n*k),在本题中如果k>n会tl ,这是就是可以交易任意次,按题2求解

      b. 3中空间复杂度为O(n*k*2),在本题中可能会ml,所以本题中可以使用滚动数组

代码:

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if (prices.empty()||prices.size()<2)
        return 0;
         int day=prices.size();
         if(k>day) return maxprofit(prices);
         
        vector<vector<int>>global(prices.size()+1,vector<int>(k+1,0));
        vector<vector<int>>local(2,vector<int>(k+1,0));
       
      
        for (int i=1; i<=day; i++)
        {
            int diff=prices[i-1]-prices[i-2];
            if (i==1) diff=0;
            
            for (int j=1; j<k+1; j++)
            {
                local[i%2][j]=max(local[(i+1)%2][j]+diff,global[i-1][j-1]+max(0,diff));
                global[i][j]=max(local[i%2][j],global[i-1][j]);
            }
        }
        return global[day][k];
        
      
        
    }
    
    int maxprofit(vector<int>& prices)
    {
        int ans=0;
        for (int i=1; i<prices.size(); i++)
        {
            ans+=max(0,prices[i]-prices[i-1]);
        }
        return ans;
    }
    
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值