给定一个包含股票价格和交易费用的正整数数组,任务是找到最大利润以及获取最大利润的天数之差。
例子:
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,2,8,4},transactionFee = 2
- 如果我们在同一天进行买卖,我们将不会获得任何利润,这就是买卖之间的差额必须至少为1的原因。
- 以1天的差额计算,如果我们买入1卢比的股票并以1天的差额卖出7卢比,这意味着在第2天购买,然后在第二天卖出,则在支付了2卢比的交易费用后,即7- 1-2 = 4,我们将获得4卢比的利润,就像我们在第4天购买并在第5天将其卖出的价格与第1天的差额一样,那么我们将获得4卢比的利润。因此,总利润为8卢比。
- 相差2天,我们将不会获得任何利润。
- 与3天的差额,如果我们买入1卢比的股票并以8天的差额卖出8卢比,这意味着在第2天进行购买,并在3天后出售,那么在支付了2卢比的交易费后,便获得最大利润,即8- 1-2 = 5我们将获得5卢比的利润。
- 与4天的差额,如果我们买入1卢比的股票并卖出4卢比,而4天的差额则意味着在第2天购买,然后在4天后卖出,则在支付了2卢比的交易费用后,即4-1 -2 = 1,我们将获得1卢比的利润。
- 相差5天,我们将不会获得任何利润。
方法:
- 遍历整个数组,每天有不同的时间。
- 通过减去包括交易费在内的每一天的价格来检查利润。
- 跟踪最大利润,并存储获得最大利润的diff_days。
- 重复上述步骤,直到循环终止。
下面是上述方法的实现:
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
输出:
8, 1
时间复杂度: O(N 2 )