660. Maximum Profitable Triplets With Increasing Prices II

Given two 0-indexed arrays prices and profits of length n. There are n items in a store where the ith item has a price of prices[i] and a profit of profits[i].

You need to select three items with the following condition:

  • prices[i] < prices[j] < prices[k]  where  i < j < k.

If you choose items with indices i, j, and k satisfying the above condition, the total profit will be profits[i] + profits[j] + profits[k].

Return the maximum total profit achievable by selecting three items that meet the condition, or return -1 if it is not possible to select such a triplet.

Example 1:

Input : prices = [5, 1, 3, 7],  profits = [10, 2, 4, 8]

Output : 14

Explanation :

The valid triplet is the items with indices 1, 2, and 3. Their prices are 1, 3, and 7 respectively (satisfying 1 < 3 < 7), and the total profit is 2 + 4 + 8 = 14

Example 2:

Input : prices = [2, 4, 6, 8, 10],  profits = [3, 5, 7, 9, 11]

Output : 27

Explanation :

Since the prices are already in increasing order, any triplet with indices in increasing order is valid. The maximum profit is obtained by selecting indices 2, 3, and 4, giving a profit of 7 + 9 + 11 = 27.

Now Your Turn!

Pick the correct output for the given input

Input : prices = [8, 6, 4, 2],  profits = [10, 20, 30, 40]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 3 <= prices.length == profits.length <= 50000
  • 1 <= prices[i] <= 5000
  • 1 <= profits[i] <= 106

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxProfit(vector<int> &prices, vector<int> &profits) {
// Your code goes here
}
};
 
Test Case

Input:

Prices
Profits