Maximum Profitable Triplets With Increasing Prices II - Problem

You are 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].

We need to pick exactly three items with the following condition: prices[i] < prices[j] < prices[k] where i < j < k.

If we pick items with indices i, j, and k satisfying the above condition, the profit would be profits[i] + profits[j] + profits[k].

Return the maximum profit we can get, or -1 if it's not possible to pick three items with the given condition.

Input & Output

Example 1 — Basic Valid Triplet
$ Input: prices = [10,20,30,40], profits = [20,30,40,50]
Output: 120
💡 Note: Pick indices (0,1,2): prices 10 < 20 < 30, profits 20+30+40 = 90. Pick indices (0,1,3): prices 10 < 20 < 40, profits 20+30+50 = 100. Pick indices (0,2,3): prices 10 < 30 < 40, profits 20+40+50 = 110. Pick indices (1,2,3): prices 20 < 30 < 40, profits 30+40+50 = 120. Maximum is 120.
Example 2 — No Valid Triplet
$ Input: prices = [4,3,2,1], profits = [33,20,19,56]
Output: -1
💡 Note: Prices are in decreasing order, so no triplet (i,j,k) exists where prices[i] < prices[j] < prices[k].
Example 3 — Mixed Order
$ Input: prices = [1,7,3,10,5], profits = [5,10,3,6,4]
Output: 21
💡 Note: Pick indices (0,1,3): prices 1 < 7 < 10, profits 5+10+6 = 21. Pick indices (0,2,3): prices 1 < 3 < 10, profits 5+3+6 = 14. Best valid triplet gives profit 21.

Constraints

  • 3 ≤ prices.length = profits.length ≤ 2000
  • 1 ≤ prices[i] ≤ 5000
  • 1 ≤ profits[i] ≤ 106

Visualization

Tap to expand
Maximum Profitable Triplets INPUT prices[] 10 20 30 40 i=0 i=1 i=2 i=3 profits[] 20 30 40 50 Condition Required: prices[i] < prices[j] < prices[k] where i < j < k Goal: Maximize profits[i]+profits[j]+profits[k] n = 4 items ALGORITHM STEPS 1 Fix Middle Element j Iterate j from 1 to n-2 2 Find Best Left (i < j) Max profit where price < prices[j] 3 Find Best Right (k > j) Max profit where price > prices[j] 4 Calculate Total Profit Sum profits, track maximum Best Triplet Found: Index Price Profit i=0 10 20 j=1 20 30 k=3 40 50 10 < 20 < 40 [OK] 20 + 30 + 50 = 100 FINAL RESULT Optimal Triplet Selection i=0 P:10 +20 --> j=2 P:30 +40 --> k=3 P:40 +50 Price Condition: 10 < 30 < 40 [OK] Profit Calculation: profits[0] = 20 profits[2] = 40 profits[3] = 50 Maximum Profit: 120 Key Insight: By fixing the middle element j, we decompose the problem into finding the best left element (i < j with smaller price) and best right element (k > j with larger price). Using a Segment Tree or Binary Indexed Tree enables O(n log n) time complexity to query max profit for price ranges efficiently. TutorialsPoint - Maximum Profitable Triplets With Increasing Prices II | Fix Middle Element Approach
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen