📌  相关文章
📜  以交易费买卖股票后的最大利润| 2套

📅  最后修改于: 2021-09-17 07:48:41             🧑  作者: Mango

给定一个表示股票价格的正整数数组arr[]和一个整数transactionFee ,任务是在多次买卖股票并给出每笔交易的交易费用后找到可能的最大利润

例子:

朴素的方法:请参阅上一篇文章,了解解决问题的最简单方法。

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

高效的方法:为了优化上述方法,思想是使用动态规划。对于每一天,如果当天买入股票(买入) 则保持最大利润,如果当天卖出所有股票(卖出) 则保持最大利润。每一天,更新的购买和使用以下关系

下面是上述方法的实现:

C++
// C++ program for the above appproach
#include 
using namespace std;
 
// Function to find the maximum
// profit with transaction fee
int MaxProfit(int arr[], int n,
              int transactionFee)
{
 
    int buy = -arr[0];
    int sell = 0;
 
    // Traversing the stocks for
    // each day
    for (int i = 1; i < n; i++) {
        int temp = buy;
 
        // Update buy and sell
        buy = max(buy, sell - arr[i]);
        sell = max(sell,
                   temp + arr[i] - transactionFee);
    }
 
    // Return the maximum profit
    return max(sell, buy);
}
 
// Driver code
int main()
{
    // Given Input
    int arr[] = { 6, 1, 7, 2, 8, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int transactionFee = 2;
 
    // Function Call
    cout << MaxProfit(arr, n, transactionFee);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the maximum
    // profit with transaction fee
    static int MaxProfit(int arr[], int n,
                         int transactionFee)
    {
 
        int buy = -arr[0];
        int sell = 0;
 
        // Traversing the stocks for
        // each day
        for (int i = 1; i < n; i++) {
            int temp = buy;
 
            // Update buy and sell
            buy = Math.max(buy, sell - arr[i]);
            sell = Math.max(sell,
                            temp + arr[i] - transactionFee);
        }
 
        // Return the maximum profit
        return Math.max(sell, buy);
    }
 
    // Driver code
    public static void main(String[] args)
    {
       
        // Given Input
        int arr[] = { 6, 1, 7, 2, 8, 4 };
        int n = arr.length;
        int transactionFee = 2;
 
        // Function Call
        System.out.println(
            MaxProfit(arr, n, transactionFee));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above appproach
 
# Function to find the maximum
# profit with transaction fee
def MaxProfit(arr, n, transactionFee):
     
    buy = -arr[0]
    sell = 0
 
    # Traversing the stocks for
    # each day
    for i in range(1, n, 1):
        temp = buy
 
        # Update buy and sell
        buy = max(buy, sell - arr[i])
        sell = max(sell, temp + arr[i] -
                   transactionFee)
 
    # Return the maximum profit
    return max(sell, buy)
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    arr = [ 6, 1, 7, 2, 8, 4 ]
    n = len(arr)
    transactionFee = 2
 
    # Function Call
    print(MaxProfit(arr, n, transactionFee))
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG {
     
    // Function to find the maximum
    // profit with transaction fee
    static int MaxProfit(int[] arr, int n,
                         int transactionFee)
    {
 
        int buy = -arr[0];
        int sell = 0;
 
        // Traversing the stocks for
        // each day
        for (int i = 1; i < n; i++) {
            int temp = buy;
 
            // Update buy and sell
            buy = Math.Max(buy, sell - arr[i]);
            sell = Math.Max(sell,
                            temp + arr[i] - transactionFee);
        }
 
        // Return the maximum profit
        return Math.Max(sell, buy);
    }
     
    // Driver code
    public static void Main()
    {
       
        // Given Input
        int[] arr = { 6, 1, 7, 2, 8, 4 };
        int n = arr.Length;
        int transactionFee = 2;
 
        // Function Call
        Console.WriteLine(
            MaxProfit(arr, n, transactionFee));
    }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
8

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