📌  相关文章
📜  最多两次买卖股票的最大利润| 2套

📅  最后修改于: 2021-09-06 11:08:24             🧑  作者: Mango

给定一个数组price[]表示不同日期的股票价格,任务是使用最多允许两次交易的交易在不同日期买卖股票后找到可能的最大利润。
注意:您不能同时进行多项交易(即您必须先卖出股票才能再次买入)。

例子:

我们已经在本文中使用 O(N) 空间中的动态规划讨论了这个问题。
高效的方法:这里使用的想法是同时跟踪两个事务。两只股票的利润计算如下

  • 第一笔交易的最大利润
    • 找出我们必须从口袋里支付的最低价格 (buy1),以获得第一只股票,即当天或任何前一天的股票价格。
    • 通过卖出当天的第一只股票来找到最大的利润。
  • 第二笔交易的最大利润
    • 我们必须从口袋里支付购买第二只股票的最低价格是
buy2 = price[i] - profit1, 
// Profit 1 is the profit from 
// selling the first stock
  • 通过出售第二只股票找到最大利润(利润 2)。

举例说明:

下面是上述方法的实现:

C++
// C++ implmenetation to find
// maximum possible profit
// with at most two transactions
 
#include 
using namespace std;
 
// Function to find the maximum
// profit with two transactions
// on a given list of stock prices
int maxProfit(int price[], int n)
{
 
    // buy1 - Money lent to buy 1 stock
    // profit1 - Profit after selling
    // the 1st stock buyed.
    // buy2 - Money lent to buy 2 stocks
    // including profit of selling 1st stock
    // profit2 - Profit after selling 2 stocks
    int buy1, profit1, buy2, profit2;
 
    // Set initial buying values to
    // INT_MAX as we want to minimize it
    buy1 = buy2 = INT_MAX;
 
    // Set initial selling values to
    // zero as we want to maximize it
    profit1 = profit2 = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Money lent to buy the stock
        // should be minimum as possible.
        // buy1 tracks the minimum possible
        // stock to buy from 0 to i-1.
        buy1 = min(buy1, price[i]);
 
        // Profit after selling a stock
        // should be maximum as possible.
        // profit1 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit1 = max(profit1, price[i] - buy1);
 
        // Now for buying the 2nd stock,
        // we will integrate profit made
        // from selling the 1st stock
        buy2 = min(buy2, price[i] - profit1);
 
        // Profit after selling a 2 stocks
        // should be maximum as possible.
        // profit2 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit2 = max(profit2, price[i] - buy2);
    }
 
    return profit2;
}
 
// Driver Code
int main()
{
    int price[] = { 2, 30, 15, 10, 8, 25, 80 };
    int n = sizeof(price) / sizeof(price[0]);
    cout << "Maximum Profit = "
         << maxProfit(price, n);
    return 0;
}


Java
// Java implmenetation to find
// maximum possible profit
// with at most two transactions
import java.util.*;
 
class GFG{
 
// Function to find the maximum
// profit with two transactions
// on a given list of stock prices
static int maxProfit(int price[], int n)
{
     
    // buy1 - Money lent to buy 1 stock
    // profit1 - Profit after selling
    // the 1st stock buyed.
    // buy2 - Money lent to buy 2 stocks
    // including profit of selling 1st stock
    // profit2 - Profit after selling 2 stocks
    int buy1, profit1, buy2, profit2;
 
    // Set initial buying values to
    // Integer.MAX_VALUE as we want to
    // minimize it
    buy1 = buy2 = Integer.MAX_VALUE;
 
    // Set initial selling values to
    // zero as we want to maximize it
    profit1 = profit2 = 0;
 
    for(int i = 0; i < n; i++)
    {
 
        // Money lent to buy the stock
        // should be minimum as possible.
        // buy1 tracks the minimum possible
        // stock to buy from 0 to i-1.
        buy1 = Math.min(buy1, price[i]);
 
        // Profit after selling a stock
        // should be maximum as possible.
        // profit1 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit1 = Math.max(profit1, price[i] - buy1);
 
        // Now for buying the 2nd stock,
        // we will integrate profit made
        // from selling the 1st stock
        buy2 = Math.min(buy2, price[i] - profit1);
 
        // Profit after selling a 2 stocks
        // should be maximum as possible.
        // profit2 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit2 = Math.max(profit2, price[i] - buy2);
    }
    return profit2;
}
 
// Driver Code
public static void main(String[] args)
{
    int price[] = { 2, 30, 15, 10, 8, 25, 80 };
    int n = price.length;
     
    System.out.print("Maximum Profit = " +
                      maxProfit(price, n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implmenetation to find
# maximum possible profit
# with at most two transactions
import sys
 
# Function to find the maximum
# profit with two transactions
# on a given list of stock prices
def maxProfit(price, n):
 
    # buy1 - Money lent to buy 1 stock
    # profit1 - Profit after selling
    # the 1st stock buyed.
    # buy2 - Money lent to buy 2 stocks
    # including profit of selling 1st stock
    # profit2 - Profit after selling 2 stocks
     
    # Set initial buying values to
    # INT_MAX as we want to minimize it
    buy1, buy2 = sys.maxsize, sys.maxsize
   
    # Set initial selling values to
    # zero as we want to maximize it
    profit1, profit2 = 0, 0
   
    for i in range(n):
   
        # Money lent to buy the stock
        # should be minimum as possible.
        # buy1 tracks the minimum possible
        # stock to buy from 0 to i-1.
        buy1 = min(buy1, price[i])
   
        # Profit after selling a stock
        # should be maximum as possible.
        # profit1 tracks maximum possible
        # profit we can make from 0 to i-1.
        profit1 = max(profit1, price[i] - buy1)
   
        # Now for buying the 2nd stock,
        # we will integrate profit made
        # from selling the 1st stock
        buy2 = min(buy2, price[i] - profit1)
   
        # Profit after selling a 2 stocks
        # should be maximum as possible.
        # profit2 tracks maximum possible
        # profit we can make from 0 to i-1.
        profit2 = max(profit2, price[i] - buy2)
 
    return profit2
   
# Driver code
price = [ 2, 30, 15, 10, 8, 25, 80 ]
n = len(price)
 
print("Maximum Profit = ", maxProfit(price, n))
 
# This code is contributed by divyeshrabadiya07


C#
// C# implmenetation to find
// maximum possible profit
// with at most two transactions
using System;
 
class GFG{
 
// Function to find the maximum
// profit with two transactions
// on a given list of stock prices
static int maxProfit(int []price, int n)
{
     
    // buy1 - Money lent to buy 1 stock
    // profit1 - Profit after selling
    // the 1st stock buyed.
    // buy2 - Money lent to buy 2 stocks
    // including profit of selling 1st stock
    // profit2 - Profit after selling 2 stocks
    int buy1, profit1, buy2, profit2;
 
    // Set initial buying values to
    // int.MaxValue as we want to
    // minimize it
    buy1 = buy2 = int.MaxValue;
 
    // Set initial selling values to
    // zero as we want to maximize it
    profit1 = profit2 = 0;
 
    for(int i = 0; i < n; i++)
    {
 
        // Money lent to buy the stock
        // should be minimum as possible.
        // buy1 tracks the minimum possible
        // stock to buy from 0 to i-1.
        buy1 = Math.Min(buy1, price[i]);
 
        // Profit after selling a stock
        // should be maximum as possible.
        // profit1 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit1 = Math.Max(profit1, price[i] - buy1);
 
        // Now for buying the 2nd stock,
        // we will integrate profit made
        // from selling the 1st stock
        buy2 = Math.Min(buy2, price[i] - profit1);
 
        // Profit after selling a 2 stocks
        // should be maximum as possible.
        // profit2 tracks maximum possible
        // profit we can make from 0 to i-1.
        profit2 = Math.Max(profit2, price[i] - buy2);
    }
    return profit2;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []price = { 2, 30, 15, 10, 8, 25, 80 };
    int n = price.Length;
     
    Console.Write("Maximum Profit = " +
                   maxProfit(price, n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Maximum Profit = 100

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

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