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

📅  最后修改于: 2022-05-13 01:56:09.993000             🧑  作者: Mango

以交易费买卖股票后的最大利润

给定一个包含股票价格和交易费用的正整数数组,任务是找到最大利润和获得最大利润的天数之差。
例子:

Input: arr[] = {6, 1, 7, 2, 8, 4}
      transactionFee = 2
Output: 8 1

Input: arr[] = {7, 1, 5, 3, 6, 4}
       transactionFee = 1
Output: 5 1

解释:考虑第一个例子:arr[] = {6, 1, 7, 2, 8, 4}, transactionFee = 2

  1. 如果我们在同一天买卖,我们将不会获得任何利润,这就是为什么买卖之间的差额必须至少为 1。
  2. 1天的差价,如果我们买入1卢比的股票,卖出7卢比的差价,即第2天买入,第二天卖出,那么在支付卢比2的交易费后,即7- 1-2=4,我们将获得 4 卢比的利润,就像我们在第 4 天买入并在第 5 天卖出,但与第 1 天的差额一样,我们将获得 4 卢比的利润。所以总利润是8卢比
  3. 2天的差价,我们不会得到任何利润
  4. 3天的差价,如果我们买入1卢比的股票,卖出8卢比的差价3天,即第2天买入,3天后卖出,则支付卢比2的交易费后的最大利润即8- 1-2=5 我们将获得5 卢比的利润。
  5. 4天差价,如果我们买入1卢比的股票,4天差价卖出4卢比,即第2天买入,4天后卖出,则在支付卢比2的交易费后,即4-1。 -2=1,我们将获得1 卢比的利润。
  6. 5天的差价,我们不会得到任何利润。

方法:

  1. 以每天的差值遍历整个数组。
  2. 通过减去每天的价格(包括交易费用)来检查利润。
  3. 跟踪最大利润并存储我们获得最大利润的 diff_days。
  4. 重复上述步骤,直到循环终止。

以下是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
int max_profit(int a[],int b[],int n,int fee)
{
int i, j, profit;
 
int l, r, diff_day = 1, sum = 0;
 
//b[0] will contain the maximum profit
    b[0]=0;                
//b[1] will contain the day
//on which we are getting the maximum profit
    b[1]=diff_day;
for(i=1;i=i;j--)
        {
        //here finding the max profit
            profit=(a[r]-a[l])-fee;
     
        //if we get less then or equal to zero
        // it means we are not getting the profit
            if(profit>0)    
                {
                sum=sum+profit;
                }
            l++;
     
            r++;
            }
//check if sum is greater then maximum then store the new maximum
    if(b[0] < sum)
{
    b[0] = sum;
     
    b[1] = diff_day;
 
    }
diff_day++;
}
 
return 0;
}
 
// Driver code
int main()
{
    int arr[] = { 6, 1, 7, 2, 8, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int b[2];
    int tranFee = 2;
 
    max_profit(arr, b, n, tranFee);
 
    cout << b[0] << ", " << b[1] << endl;
 
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class solution
{
 
static int max_profit(int a[],int b[],int n,int fee)
{
int i, j, profit;
 
int l, r, diff_day = 1, sum = 0;
 
//b[0] will contain the maximum profit
    b[0]=0;                
//b[1] will contain the day
//on which we are getting the maximum profit
    b[1]=diff_day;
for(i=1;i=i;j--)
        {
        //here finding the max profit
            profit=(a[r]-a[l])-fee;
     
        //if we get less then or equal to zero
        // it means we are not getting the profit
            if(profit>0)    
                {
                sum=sum+profit;
                }
            l++;
     
            r++;
            }
//check if sum is greater then maximum then store the new maximum
    if(b[0] < sum)
{
    b[0] = sum;
     
    b[1] = diff_day;
 
    }
diff_day++;
}
 
return 0;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 6, 1, 7, 2, 8, 4 };
    int n = arr.length;
    int[] b = new int[2];
    int tranFee = 2;
 
    max_profit(arr, b, n, tranFee);
 
    System.out.println(b[0]+", "+b[1]);
 
}
}
 
//This code is contributed by Surendra_Gangwar


Python3
# Python3 implementation of above approach
def max_profit(a, b, n, fee):
 
    i, j, profit = 1, n - 1, 0
     
    l, r, diff_day = 0, 0, 1
     
    # b[0] will contain the maximum profit
    b[0] = 0   
     
    # b[1] will contain the day on which
    # we are getting the maximum profit
    b[1] = diff_day
 
    for i in range(1, n):
        l = 0
        r = diff_day
        Sum = 0
     
        for j in range(n - 1, i - 1, -1):
             
            # here finding the max profit
            profit = (a[r] - a[l]) - fee
         
            # if we get less then or equal to zero
            # it means we are not getting the profit
            if(profit > 0):
                Sum = Sum + profit
                     
            l += 1
            r += 1
                 
        # check if Sum is greater then maximum
        # then store the new maximum
        if(b[0] < Sum):
            b[0] = Sum
            b[1] = diff_day
     
    diff_day += 1
     
    return 0
     
# Driver code
arr = [6, 1, 7, 2, 8, 4]
n = len(arr)
b = [0 for i in range(2)]
tranFee = 2
 
max_profit(arr, b, n, tranFee)
 
print(b[0], ",", b[1])
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
static int max_profit(int []a, int []b,
                      int n, int fee)
{
int i, j, profit;
 
int l, r, diff_day = 1, sum = 0;
 
// b[0] will contain the
// maximum profit
b[0] = 0;
 
// b[1] will contain the day on which
// we are getting the maximum profit
b[1] = diff_day;
for(i = 1; i < n; i++)
{
    l = 0; r = diff_day; sum = 0;
 
    for(j = n - 1; j >= i; j--)
        {
            // here finding the max profit
            profit = (a[r] - a[l]) - fee;
     
            // if we get less then or equal
            // to zero it means we are not
            // getting the profit
            if(profit > 0)
            {
                sum = sum + profit;
            }
            l++;
     
            r++;
        }
         
    // check if sum is greater then maximum
    // then store the new maximum
    if(b[0] < sum)
    {
        b[0] = sum;
         
        b[1] = diff_day;
     
    }
    diff_day++;
}
 
return 0;
}
 
// Driver code
static public void Main ()
{
    int []arr = { 6, 1, 7, 2, 8, 4 };
    int n = arr.Length;
    int[] b = new int[2];
    int tranFee = 2;
     
    max_profit(arr, b, n, tranFee);
     
    Console.WriteLine(b[0] + ", " + b[1]);
}
}
 
// This code is contributed by Sachin


PHP
= $i; $j--)
        {
            // here finding the max profit
            $profit = ($a[$r] - $a[$l]) - $fee;
     
            // if we get less then or equal to zero
            // it means we are not getting the profit
            if($profit > 0)    
            {
                $sum = $sum + $profit;
            }
            $l++;
     
            $r++;
        }
         
        // check if sum is greater then maximum
        // then store the new maximum
        if($b[0] < $sum)
        {
            $b[0] = $sum;
             
            $b[1] = $diff_day;
        }
        $diff_day++;
    }
 
}
 
// Driver code
$arr = array(6, 1, 7, 2, 8, 4 );
$n = sizeof($arr);
$b = array();
$tranFee = 2;
 
max_profit($arr, $b, $n, $tranFee);
echo($b[0]);
echo(", ");
echo($b[1]);
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript


Python3
from typing import List, Tuple
 
def max_profit(prices: List[int], transaction_fee:int = 0) -> Tuple[int, int]:
    n = len(prices)
    start = 0
    end = 1
    profit = 0
    max_profit_till_now = float('-inf')
    diff = 0
    while start < n - 1 and end < n:
        while start < n - 1 and prices[start] > prices[start + 1]:
            start += 1
        end = start + 1
        while end < n - 1 and prices[end] < prices[end + 1]:
            end += 1
            if end == n:
                continue
        cur_profit = prices[end] - prices[start] - transaction_fee
        if cur_profit > 0:
            profit += cur_profit
        if max_profit_till_now < cur_profit:
            max_profit_till_now = cur_profit
            diff = end - start
        start = end + 1
    return profit, diff
print(max_profit([6, 1, 7, 2, 8, 4], 2))


输出
8, 1

时间复杂度: O(N 2 )

辅助空间: O(1)

更好的方法:

与 https://www.geeksforgeeks.org/stock-buy-sell/ 相同,但也增加了差异天数

Python3

from typing import List, Tuple
 
def max_profit(prices: List[int], transaction_fee:int = 0) -> Tuple[int, int]:
    n = len(prices)
    start = 0
    end = 1
    profit = 0
    max_profit_till_now = float('-inf')
    diff = 0
    while start < n - 1 and end < n:
        while start < n - 1 and prices[start] > prices[start + 1]:
            start += 1
        end = start + 1
        while end < n - 1 and prices[end] < prices[end + 1]:
            end += 1
            if end == n:
                continue
        cur_profit = prices[end] - prices[start] - transaction_fee
        if cur_profit > 0:
            profit += cur_profit
        if max_profit_till_now < cur_profit:
            max_profit_till_now = cur_profit
            diff = end - start
        start = end + 1
    return profit, diff
print(max_profit([6, 1, 7, 2, 8, 4], 2))

输出:

(8, 1)

时间复杂度:O(N)

辅助空间:O(1)