Problem · Array

Maximum Profit from an Increasing Price Triplet

Learn this problem
MediumOdoo logoOdooFULLTIMEOA

Problem 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[]) → int

Examples

Example 1

price = [1, 5, 3, 4, 6]profit = [2, 3, 4, 5, 6]return = 15

The 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 = -1

No three indices have strictly increasing prices.

More Odoo problems

drafts saved locally
public int getMaximumProfit(int[] price, int[] profit) {
  // Write your code here.
}
price[1, 5, 3, 4, 6]
profit[2, 3, 4, 5, 6]
expected15
checking account