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

📅  最后修改于: 2021-10-26 02:31:29             🧑  作者: 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)