Ctrl K

Rising-Price Trio

Medium
A shelf holds n items in a row. The item at position i costs prices[i] and brings a profit of profits[i]. Pick exactly three items at positions i < j < k whose prices strictly increase from left to right: prices[i] < prices[j] < prices[k]. The profit of such a trio is the sum of the three profits. Return the largest profit a trio can bring, or -1 if no valid trio exists.
Example 1
prices=[5, 1, 4, 2, 6], profits=[3, 9, 1, 4, 2] → 15

The best pick is the items priced 1, 2 and 6 (positions 1, 3 and 4): prices rise from left to right and the profit is 9 + 4 + 2 = 15.

Example 2
prices=[7, 7, 7], profits=[1, 2, 3] → -1

All prices are equal and the rise must be strict, so no valid trio exists and the answer is -1.

Example 3
prices=[2, 9, 3, 8], profits=[10, 1, 1, 1] → 12

Nothing after the price 9 costs more, so the only trio is prices 2, 3, 8 with profit 10 + 1 + 1 = 12.

Constraints
  • 3 ≤ prices.length = profits.length ≤ 2000
  • 1 ≤ prices[i] ≤ 10⁶
  • 1 ≤ profits[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRebuild the Line from Height Records MediumCounting the Longest Rising Selections MediumSteady Climb or Drop Trios MediumOnes Outnumber Zeros MediumRank of an Arrangement Medium
loading Python…
Input
prices=[5, 1, 4, 2, 6], profits=[3, 9, 1, 4, 2]
Expected
15

Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.