📜  在休息条件下买卖股票的利润最大化

📅  最后修改于: 2021-09-02 06:32:13             🧑  作者: Mango

股票每天的价格在数组arr[] 中给出,持续N天,任务是在必须先卖出股票的条件下,找到在那几天买卖股票所能获得的最大利润再次买入,不能在卖出股票的第二天买入股票。 (即至少休息一天)。

例子:

方法 :
考虑三种状态: RESTHOLDSOLD

要达到REST状态,什么都不做。
要达到HOLD状态,请购买股票。
要达到SOLD状态,请先卖出需要购买的股票。
通过以上动作,就做出了一个转移图。

通过使用此转换图,可以找出利润。
i天:

  • rest[i]表示在第 i 天休息所获得的最大利润。由于i一天休息日,股价可能已经卖了(I-1)天或可能不会被出售。 rest[i] = max(rest[i-1], sell[i-1]) 的值
  • 保持通过购买由[i]表示最大利润上的i天或通过在某一天的购买前的i一天休息我。所以, hold[i] = max(hold[i-1], rest[i-1]+price[i]) 的值
  • sell[i]表示i 天卖出的最大利润。股票必须日卖它在我之前已经买了一些日子。因此,已售出[i] = 持有[i-1] + 价格[i]

因此,最终的答案将是

下面是上述方法的实现:

C++
// C++ program for the above problem
#include 
using namespace std;
 
int maxProfit(int prices[], int n)
{
    // If there is only one day
    // for buying and selling
    // no profit can be made
    if (n <= 1)
        return 0;
 
    // Array to store Maxprofit by
    // resting on given day
    int rest[n] = { 0 };
 
    // Array to store Maxprofit by
    // buying or resting on the
    // given day
    int hold[n] = { 0 };
 
    // Array to store Maxprofit by
    // selling on given day
    int sold[n] = { 0 };
 
    // Initially there will 0 profit
    rest[0] = 0;
 
    // Buying on 1st day results
    // in negative profit
    hold[0] = -prices[0];
 
    // zero profit since selling
    // before buying isn't possible
    sold[0] = 0;
 
    for (int i = 1; i < n; i++) {
 
        // max of profit on (i-1)th
        // day by resting and profit
        // on (i-1)th day by selling.
        rest[i] = max(rest[i - 1],
                      sold[i - 1]);
 
        // max of profit by resting
        // on ith day and
        // buying on ith day.
        hold[i] = max(hold[i - 1],
                      rest[i - 1]
                          - prices[i]);
 
        // max of profit by selling
        // on ith day
        sold[i] = hold[i - 1] + prices[i];
    }
 
    // maxprofit
    return max(rest[n - 1],
               sold[n - 1]);
}
 
// Driver Code
int main()
{
    int price[] = { 2, 4,
                    5, 0, 2 };
    int n = sizeof(price)
            / sizeof(price[0]);
    cout << maxProfit(price, n)
         << endl;
    return 0;
}


Java
// Java program for the above problem
class GFG{
 
static int maxProfit(int prices[], int n)
{
     
    // If there is only one day
    // for buying and selling
    // no profit can be made
    if (n <= 1)
        return 0;
 
    // Array to store Maxprofit by
    // resting on given day
    int rest[] = new int[n];
 
    // Array to store Maxprofit by
    // buying or resting on the
    // given day
    int hold[] = new int[9];
 
    // Array to store Maxprofit by
    // selling on given day
    int sold[] = new int[9];
 
    // Initially there will 0 profit
    rest[0] = 0;
 
    // Buying on 1st day results
    // in negative profit
    hold[0] = -prices[0];
 
    // Zero profit since selling
    // before buying isn't possible
    sold[0] = 0;
 
    for(int i = 1; i < n; i++)
    {
        
       // max of profit on (i-1)th
       // day by resting and profit
       // on (i-1)th day by selling.
       rest[i] = Math.max(rest[i - 1],
                          sold[i - 1]);
        
       // max of profit by resting
       // on ith day and
       // buying on ith day.
       hold[i] = Math.max(hold[i - 1],
                          rest[i - 1] -
                        prices[i]);
        
       // max of profit by selling
       // on ith day
       sold[i] = hold[i - 1] + prices[i];
    }
     
    // maxprofit
    return Math.max(rest[n - 1],
                    sold[n - 1]);
}
 
// Driver Code
public static void main(String[] args)
{
    int price[] = { 2, 4, 5, 0, 2 };
    int n = price.length;
     
    System.out.print(maxProfit(price, n) + "\n");
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above problem
def maxProfit(prices, n):
 
    # If there is only one day
    # for buying and selling
    # no profit can be made
    if (n <= 1):
        return 0
 
    # Array to store Maxprofit by
    # resting on given day
    rest = [0] * n
 
    # Array to store Maxprofit by
    # buying or resting on the
    # given day
    hold = [0] * n
 
    # Array to store Maxprofit by
    # selling on given day
    sold = [0] * n
 
    # Initially there will 0 profit
    rest[0] = 0
 
    # Buying on 1st day results
    # in negative profit
    hold[0] = -prices[0]
 
    # zero profit since selling
    # before buying isn't possible
    sold[0] = 0
 
    for i in range(1, n):
 
        # max of profit on (i-1)th
        # day by resting and profit
        # on (i-1)th day by selling.
        rest[i] = max(rest[i - 1],
                      sold[i - 1])
 
        # max of profit by resting
        # on ith day and
        # buying on ith day.
        hold[i] = max(hold[i - 1],
                      rest[i - 1] -
                    prices[i])
 
        # max of profit by selling
        # on ith day
        sold[i] = hold[i - 1] + prices[i]
     
    # maxprofit
    return max(rest[n - 1],
               sold[n - 1])
 
# Driver Code
price = [ 2, 4, 5, 0, 2 ]
n = len(price)
 
print(maxProfit(price, n))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above problem
using System;
 
class GFG{
     
static int maxProfit(int[] prices, int n)
{
     
    // If there is only one day
    // for buying and selling
    // no profit can be made
    if (n <= 1)
        return 0;
 
    // Array to store Maxprofit by
    // resting on given day
    int[] rest = new int[n];
 
    // Array to store Maxprofit by
    // buying or resting on the
    // given day
    int[] hold = new int[9];
 
    // Array to store Maxprofit by
    // selling on given day
    int[] sold = new int[9];
 
    // Initially there will 0 profit
    rest[0] = 0;
 
    // Buying on 1st day results
    // in negative profit
    hold[0] = -prices[0];
 
    // Zero profit since selling
    // before buying isn't possible
    sold[0] = 0;
 
    for(int i = 1; i < n; i++)
    {
         
        // max of profit on (i-1)th
        // day by resting and profit
        // on (i-1)th day by selling.
        rest[i] = Math.Max(rest[i - 1],
                           sold[i - 1]);
             
        // max of profit by resting
        // on ith day and
        // buying on ith day.
        hold[i] = Math.Max(hold[i - 1],
                           rest[i - 1] -
                         prices[i]);
             
        // max of profit by selling
        // on ith day
        sold[i] = hold[i - 1] + prices[i];
    }
     
    // maxprofit
    return Math.Max(rest[n - 1],
                    sold[n - 1]);
}
 
// Driver code
static void Main()
{
    int[] price = { 2, 4, 5, 0, 2 };
    int n = price.Length;
 
    Console.WriteLine(maxProfit(price, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live