Quick Overview

Given arrays of item prices and profits, choose three indices in order whose prices strictly increase and return the largest total profit, or -1 when no such triplet exists. Tests handling equal prices and finding a faster than quadratic solution for arrays of up to 50,000 items.

Maximum total profit of three items with strictly increasing prices

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given two integer arrays `price` and `profit` of equal length `n`, where `price[i]` and `profit[i]` are the price and profit of the `i`-th item. Choose a triplet of indices `(i, j, k)` such that `i < j < k` and `price[i] < price[j] < price[k]` (prices must be strictly increasing). The profit of the triplet is `profit[i] + profit[j] + profit[k]`. Return the maximum profit over all valid triplets, or `-1` if no such triplet exists. ### Function Signature ```python def max_triplet_profit(price: list[int], profit: list[int]) -> int: ``` ### Rules - Indices are 0-based and must satisfy `i < j < k`. - Prices must be strictly increasing along the triplet. Two items with the same price can never appear in the same triplet. - Return `-1` when no triplet meets both conditions. ### Constraints - `3 <= n <= 5 * 10^4` - `price.length == profit.length == n` - `1 <= price[i] <= 10^9` - `1 <= profit[i] <= 10^6` ### Examples ```text Input: price = [10, 2, 3, 4], profit = [100, 2, 7, 10] Output: 19 ``` The only valid triplet is `(1, 2, 3)` with prices 2 < 3 < 4, and its profit is 2 + 7 + 10 = 19. The item with profit 100 comes first and has the highest price, so no later item can follow it. ```text Input: price = [4, 3, 2, 1], profit = [33, 20, 19, 87] Output: -1 ``` The prices are strictly decreasing, so no valid triplet exists. ```text Input: price = [5, 1, 5, 2, 5, 3], profit = [6, 1, 8, 2, 9, 3] Output: 12 ``` The valid triplets are `(1, 3, 4)` with prices 1 < 2 < 5 and `(1, 3, 5)` with prices 1 < 2 < 3, worth 12 and 6. The three items priced 5 would add up to 23, but their prices are equal, so they do not form a valid triplet.

Overview: Given arrays of item prices and profits, choose three indices in order whose prices strictly increase and return the largest total profit, or -1 when no such triplet exists. Tests handling equal prices and finding a faster than quadratic solution for arrays of up to 50,000 items.

You are given two integer arrays `price` and `profit` of equal length `n`. Item `i` has price `price[i]` and profit `profit[i]`. Choose a triplet of indices `(i, j, k)` with `i < j < k` and `price[i] < price[j] < price[k]`, so prices strictly increase along the triplet. The profit of the triplet is `profit[i] + profit[j] + profit[k]`. Return the maximum profit over all valid triplets, or `-1` if no valid triplet exists. ### Rules - Indices are 0-based and must satisfy `i < j < k`. - Prices must strictly increase along the triplet. Two items with the same price can never appear in the same triplet. - Return `-1` when no triplet meets both conditions. - The result is a single integer. No value exceeds 2^31 - 1: prices are at most 10^9 and the largest possible answer is 3 * 10^6. ### Constraints - `3 <= n <= 5 * 10^4` - `price.length == profit.length == n` - `1 <= price[i] <= 10^9` - `1 <= profit[i] <= 10^6` ### Example 1 ```text Input: price = [10, 2, 3, 4], profit = [100, 2, 7, 10] Output: 19 ``` The only valid triplet is `(1, 2, 3)` with prices 2 < 3 < 4, and its profit is 2 + 7 + 10 = 19. The item with profit 100 comes first and has the highest price, so no later item can follow it. ### Example 2 ```text Input: price = [5, 1, 5, 2, 5, 3], profit = [6, 1, 8, 2, 9, 3] Output: 12 ``` The valid triplets are `(1, 3, 4)` with prices 1 < 2 < 5 and `(1, 3, 5)` with prices 1 < 2 < 3, worth 12 and 6. The three items priced 5 would add up to 23, but their prices are equal, so they do not form a valid triplet.

Constraints

  • 3 <= n <= 5 * 10^4
  • price.length == profit.length == n
  • 1 <= price[i] <= 10^9
  • 1 <= profit[i] <= 10^6

Examples

Input: ([10, 2, 3, 4], [100, 2, 7, 10])

Expected Output: 19

Explanation: Source example 1: the profit-100 item comes first with the highest price, so only (1, 2, 3) is valid, worth 2 + 7 + 10 = 19.

Input: ([4, 3, 2, 1], [33, 20, 19, 87])

Expected Output: -1

Explanation: Source example 2: strictly decreasing prices admit no triplet.

Hints

  1. Fix the middle item of the triplet. What single number do you need from the items before it, and what from the items after it?
  2. Two items with the same price can never share a triplet, so every price comparison on both sides has to be strict.
  3. With n up to 5 * 10^4, trying every triplet or every pair is too slow; aim for a running time well below O(n^2).

Maximum Profit Triplet with Strictly Increasing Prices

hardSalesforceSoftware Engineer

You are given two integer arrays price and profit of equal length n. Item i has price price[i] and profit profit[i].

Choose a triplet of indices (i, j, k) with i < j < k and price[i] < price[j] < price[k], so prices strictly increase along the triplet. The profit of the triplet is profit[i] + profit[j] + profit[k].

Return the maximum profit over all valid triplets, or -1 if no valid triplet exists.

Rules

  • Indices are 0-based and must satisfy i < j < k.
  • Prices must strictly increase along the triplet. Two items with the same price can never appear in the same triplet.
  • Return -1 when no triplet meets both conditions.
  • The result is a single integer. No value exceeds 2^31 - 1: prices are at most 10^9 and the largest possible answer is 3 * 10^6.

Constraints

  • 3 <= n <= 5 * 10^4
  • price.length == profit.length == n
  • 1 <= price[i] <= 10^9
  • 1 <= profit[i] <= 10^6

Example 1

Input: price = [10, 2, 3, 4], profit = [100, 2, 7, 10]
Output: 19

The only valid triplet is (1, 2, 3) with prices 2 < 3 < 4, and its profit is 2 + 7 + 10 = 19. The item with profit 100 comes first and has the highest price, so no later item can follow it.

Example 2

Input: price = [5, 1, 5, 2, 5, 3], profit = [6, 1, 8, 2, 9, 3]
Output: 12

The valid triplets are (1, 3, 4) with prices 1 < 2 < 5 and (1, 3, 5) with prices 1 < 2 < 3, worth 12 and 6. The three items priced 5 would add up to 23, but their prices are equal, so they do not form a valid triplet.

Examples

Example 1
Input
([10, 2, 3, 4], [100, 2, 7, 10])
Output
19
Notes
Source example 1: the profit-100 item comes first with the highest price, so only (1, 2, 3) is valid, worth 2 + 7 + 10 = 19.
Example 2
Input
([4, 3, 2, 1], [33, 20, 19, 87])
Output
-1
Notes
Source example 2: strictly decreasing prices admit no triplet.

Constraints

  • •3 <= n <= 5 * 10^4
  • •price.length == profit.length == n
  • •1 <= price[i] <= 10^9
  • •1 <= profit[i] <= 10^6

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_triplet_profit(price: list[int], profit: list[int]) -> int:
Sample call
max_triplet_profit([10, 2, 3, 4], [100, 2, 7, 10])
Language
Case 1
Input
([10, 2, 3, 4], [100, 2, 7, 10])
Expected
19
Case 2
Input
([4, 3, 2, 1], [33, 20, 19, 87])
Expected
-1

Browse More Questions

Show the approach

Approach

Treat each index j as the middle item. The best triplet with middle j is bestLeft(j) + profit[j] + bestRight(j), where bestLeft(j) is the largest profit[i] with i < j and price[i] < price[j], and bestRight(j) is the largest profit[k] with k > j and price[k] > price[j].

Compress the distinct prices to ranks 1 to m in increasing order. Scan left to right with a Fenwick tree that stores prefix maxima of profit by rank. Before inserting item j, a prefix query over ranks 1 to rank(j) - 1 returns the best profit among earlier items with a strictly smaller price; items with the same price share rank(j), so they are excluded. Then scan right to left with a fresh Fenwick tree over reversed ranks m + 1 - rank(j), so the same prefix query returns the best profit among later items with a strictly larger price. Every profit is at least 1, so a query result of 0 means that side has no qualifying item and j cannot be a middle. The answer is the largest three-term sum over all j that have both sides, or -1 when no j does.

Correctness: any valid triplet (i, j, k) has profit at most bestLeft(j) + profit[j] + bestRight(j), and that sum is itself the profit of a valid triplet, because the chosen left item has a smaller index and a strictly smaller price and the chosen right item has a larger index and a strictly larger price. Taking the maximum over all middles gives the exact optimum.

Edge cases: when all prices are equal, or prices never increase, no middle has both sides and the result is -1. Equal prices are never combined because both queries are strict. The largest answer is 3 * 10^6, so 32-bit integers suffice in every language.

Time complexity:
O(n log n)
Space complexity:
O(n)