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

📅  最后修改于: 2021-05-08 16:53:54             🧑  作者: Mango

每天的价格以Nrr数组arr []的形式给出N天,任务是找到在那些天之前必须出售股票的条件下,通过购买和出售股票所能获得的最大利润再次购买,第二天不能出售股票。 (即,至少休息一天)。

例子:

方法 :
考虑三种状态: RESTHOLDSOLD

要达到REST状态,请不要执行任何操作。
要达到“持有”状态,请购买股票。
为了达到售出状态,销售为这首有需要购买的股票。
通过以上动作,制作了过渡图。

通过使用此过渡图,可以发现利润。
第i天:

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


输出:
4


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