Problem · Array
Maximum Profit from an Increasing Price Triplet
Learn this problemProblem statement
An analyst observes a stock over n days. The stock price on day i is price[i], and the profit associated with that day is profit[i].
Choose three indices i < j < k such that price[i] < price[j] < price[k]. Return the maximum possible value of profit[i] + profit[j] + profit[k]. If no valid triplet exists, return -1.
Function
getMaximumProfit(price: int[], profit: int[]) → intExamples
Example 1
price = [1, 5, 3, 4, 6]profit = [2, 3, 4, 5, 6]return = 15The optimal triplet is (3, 4, 5) in one-based indexing. Its prices satisfy 3 < 4 < 6, and its total profit is 4 + 5 + 6 = 15.
Example 2
price = [3, 2, 1]profit = [10, 20, 30]return = -1No three indices have strictly increasing prices.