📌  相关文章
📜  通过每天根据给定的利率交易股票来最大化利润

📅  最后修改于: 2021-06-25 23:04:30             🧑  作者: Mango

给定一个由n个正整数组成的数组arr [] ,它表示在N天中的每一天买卖股票的成本。任务是找到在特定日期购买股票或出售所有先前购买的股票所能获得的最大利润。
例子:

方法:想法是将给定的股票价格分成不同的子数组,以使每个子数组在数组末尾具有最大值。然后,通过从每个子数组中的最后一个元素减去每个股票值来找到利润。步骤如下:

  1. 从末尾遍历数组arr []并将arr [N – 1]视为股票的当前最高价格,例如maxM
  2. 只要价格最高,以前购买的所有股票都将获得利润。因此,在arr []中向左移动,并继续为所有索引添加maxM – arr [i]以获利,直到任何索引的成本高于maxM为止
  3. 如果遇到高于maxM的价格,则购买会造成损失。因此,将特定日的库存成本(即arr [i])设置为maxM的当前值,然后重复步骤2
  4. 遍历数组后,在arr []中为每个可能的价格打印在步骤2中获得的差异总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum profit
int maxProfit(int* prices, int n)
{
    int profit = 0, currentDay = n - 1;
 
    // Start from the last day
    while (currentDay > 0) {
 
        int day = currentDay - 1;
 
        // Traverse and keep adding the
        // profit until a day with
        // price of stock higher
        // than currentDay is obtained
        while (day >= 0
            && (prices[currentDay]
                > prices[day])) {
 
            profit += (prices[currentDay]
                    - prices[day]);
 
            day--;
        }
 
        // Set this day as currentDay
        // with maximum cost of stock
        // currently
        currentDay = day;
    }
 
    // Return the profit
    return profit;
}
 
// Driver Code
int main()
{
    // Given array of prices
    int prices[] = { 2, 3, 5 };
 
    int N = sizeof(prices) / sizeof(prices[0]);
 
    // Function Call
    cout << maxProfit(prices, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the maximum profit
static int maxProfit(int[] prices, int n)
{
    int profit = 0, currentDay = n - 1;
 
    // Start from the last day
    while (currentDay > 0)
    {
        int day = currentDay - 1;
 
        // Traverse and keep adding the
        // profit until a day with
        // price of stock higher
        // than currentDay is obtained
        while (day >= 0 &&
            (prices[currentDay] > prices[day]))
        {
            profit += (prices[currentDay] -
                    prices[day]);
 
            day--;
        }
 
        // Set this day as currentDay
        // with maximum cost of stock
        // currently
        currentDay = day;
    }
 
    // Return the profit
    return profit;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array of prices
    int prices[] = { 2, 3, 5 };
 
    int N = prices.length;
 
    // Function Call
    System.out.print(maxProfit(prices, N));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to implement
# above approach
 
# Function to find the maximum profit
def maxProfit(prices, n):
 
    profit = 0
    currentDay = n - 1
 
    # Start from the last day
    while (currentDay > 0):
        day = currentDay - 1
 
        # Traverse and keep adding the
        # profit until a day with
        # price of stock higher
        # than currentDay is obtained
        while (day >= 0 and
              (prices[currentDay] >
               prices[day])):
            profit += (prices[currentDay] -
                       prices[day])
                        
            day -= 1
         
        # Set this day as currentDay
        # with maximum cost of stock
        # currently
        currentDay = day;
     
    # Return the profit
    return profit;
 
# Driver Code
 
# Given array of prices
prices = [ 2, 3, 5 ]
 
N = len(prices)
 
# Function call
print(maxProfit(prices, N))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum profit
static int maxProfit(int[] prices, int n)
{
    int profit = 0, currentDay = n - 1;
 
    // Start from the last day
    while (currentDay > 0)
    {
        int day = currentDay - 1;
 
        // Traverse and keep adding the
        // profit until a day with
        // price of stock higher
        // than currentDay is obtained
        while (day >= 0 &&
              (prices[currentDay] > prices[day]))
        {
            profit += (prices[currentDay] -
                       prices[day]);
 
            day--;
        }
 
        // Set this day as currentDay
        // with maximum cost of stock
        // currently
        currentDay = day;
    }
 
    // Return the profit
    return profit;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array of prices
    int []prices = { 2, 3, 5 };
 
    int N = prices.Length;
 
    // Function call
    Console.Write(maxProfit(prices, N));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
5

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。