Description
Solutions
Stock Maximum Profit
📚RELATED PROBLEMS
An analyst is analyzing a stock over a period of n
days. The price of the stock on the ith
day is price[i]
, and the profit obtained is denoted by profit[i]
.
The analyst wants to pick a triplet of days (i, j, k)
such that (i < j < k)
and price[i] < price[j] < price[k]
in such a way that the total profit, i.e. profit[i] + profit[j] + profit[k]
is maximized.
Find the maximum total profit possible. If there is no valid triplet, return -1
.
Function Description
Complete the function getMaximumProfit
which has the following parameters:
int price[n]
: the prices of the stock on each dayint profit[n]
: the profits obtained from the stock on each dayReturns
long_int
: the maximum possible total profitExample 1:
Input: price = [2, 3, 1, 5, 9], profit = [1, 2, 6, 1, 5]
Output: 12
Explanation:An optimal triplet (considering 1-based indexing) is (3, 4, 5). Here 1 < 5 < 9, and total profit = 6 + 1 + 5 = 12.
Example 2:
Input: price = [4, 3, 2, 1], profit = [4, 3, 2, 1]
Output: -1
Explanation:There is no valid triplet.
Constraints:
1 <= n <= 4000
1 <= price[i], profit[i] <= 109
Related Problems
Testcase
Result
Case 1
input:
output: