Say you have an array, A, for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most 2 transactions.Return the maximum possible profit.Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).Problem Constraints1 <= length(A) <= 7e51 <= A[i] <= 1e7Sample input57 2 4 8 7Sample output6
Question
Say you have an array, A, for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most 2 transactions.Return the maximum possible profit.Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).Problem Constraints1 <= length(A) <= 7e51 <= A[i] <= 1e7Sample input57 2 4 8 7Sample output6
Solution
The problem can be solved using dynamic programming. Here is a step-by-step guide to the algorithm:
-
Initialize two arrays,
profit1andprofit2, of the same length as the input arrayA. These arrays will store the maximum profit that can be made with at most one transaction and at most two transactions, respectively. -
Set
profit1[0]to 0, because no profit can be made on the first day. For each dayifrom 1 to the end ofA, calculateprofit1[i]as the maximum ofprofit1[i-1](the maximum profit made up to the previous day) andA[i] - min_price, wheremin_priceis the minimum price seen so far. This represents the maximum profit that can be made with a single transaction up to dayi. -
Set
profit2[0]andprofit2[1]to 0, because no profit can be made on the first two days with two transactions. For each dayifrom 2 to the end ofA, calculateprofit2[i]as the maximum ofprofit2[i-1](the maximum profit made up to the previous day with two transactions) andA[i] - min_price_with_profit, wheremin_price_with_profitis the minimum price seen so far minus the profit that can be made with one transaction up to that day. This represents the maximum profit that can be made with two transactions up to dayi. -
The maximum profit that can be made with at most two transactions is
profit2[-1], the last element ofprofit2.
Here is a Python implementation of the algorithm:
def maxProfit(A):
if not A:
return 0
profit1 = [0] * len(A)
profit2 = [0] * len(A)
min_price = A[0]
for i in range(1, len(A)):
min_price = min(min_price, A[i])
profit1[i] = max(profit1[i-1], A[i] - min_price)
min_price_with_profit = A[0] - profit1[0]
for i in range(2, len(A)):
min_price_with_profit = min(min_price_with_profit, A[i] - profit1[i-1])
profit2[i] = max(profit2[i-1], A[i] - min_price_with_profit)
return profit2[-1]
For the sample input A = [5, 7, 2, 4, 8, 7], the function maxProfit(A) returns 6, which is the maximum profit that can be made with at most two transactions.
Similar Questions
You are given an array of integers 'prices' of size 'n', where ‘prices[i]’ is the price of a given stock on an ‘i’-th day. You want to maximize the profit by choosing a single day to buy one stock and a different day to sell that stock.Please note that you can’t sell a stock before you buy one.Detailed explanation ( Input/output format, Notes, Images )Constraints :1 <= 'n' <= 10 ^ 51 <= ‘prices[i]’<= 10 ^ 9Time Limit: 1 secSample Input 1:67 1 5 4 3 6Sample Output 1 :5Explanation Of Sample Input 1:Purchase stock on day two, where the price is one, and sell it on day six, where the price is 6. Profit = 6 - 1 = 5.Sample Input 2:55 4 3 2 1Sample Output 2 :0
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.Find and return the maximum profit you can achieve.
Finding the Maximum Stock PriceYou are an analyst tracking the stock prices of a company over the past week (7 days). Write a program that reads the stock prices into an array and finds the maximum stock price during this period.Constraints:NAExample:Sample Input:1001021059998110107Sample Output:110
You are given an array A of size NLet M be the minimum value present in the array initiallyIn one operation, you can choose an element Ai ( 1 ≤ i ≤ N ) and an integer X ( 1 ≤ X ≤ 100 ), and set Ai = X.Determine the minimum number of operations required to make M the maximum value in the array A.
Given a set of N jobs where each jobi has a deadline and profit associated with it.Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit associated with job if and only if the job is completed by its deadline.Find the number of jobs done and the maximum profit.Note: Jobs will be given in the form (Jobid, Deadline, Profit) associated with that Job.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.