给定一个由n个正整数组成的数组arr [] ,它表示在N天的每一天买卖股票的成本。任务是找到在特定日期购买股票或出售所有先前购买的股票所能获得的最大利润。
例子:
Input: arr[] = {2, 3, 5}
Output: 5
Explanation:
Price on Day 1 = 2. Therefore buy the stocks on Day 1 at this cost. Total_Spent = 2
Price on Day 2 = 3. Therefore buy the stocks on Day 2 at this cost. Total_Spent = 2 + 3 = 5
Price on Day 3 = 5. If the stocks are sold at this price, the profit will be maximum. Therefore sell the bought stocks on Day 3 at this cost. Total_gained = 5*2 = 10
Profit = 10 – 5 = 5
Input: arr[] = {8, 5, 1}
Output: 0
Explanation:
After buying any stock we can’t sell on any other day because it will lead to loss. Therefore Profit is 0.
方法:想法是将给定的股票价格分成不同的子数组,以使每个子数组在数组末尾具有最大值。然后,通过从每个子数组中的最后一个元素减去每个股票值来找到利润。步骤如下:
- 从末尾遍历数组arr []并将arr [N – 1]视为股票的当前最高价格,例如maxM 。
- 只要价格最高,以前购买的所有股票都将获得利润。因此,在arr []中向左移动,并继续为所有索引添加maxM – arr [i]以获利,直到任何索引的成本高于maxM为止。
- 如果遇到高于maxM的价格,则购买会造成损失。因此,将特定日的库存成本(即arr [i])设置为maxM的当前值,然后重复步骤2 。
- 遍历数组后,在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)