659. Maximum Increasing Triplet Value

Given an array nums, return the maximum value of a triplet (i, j, k) such that: i < j < k and nums[i] < nums[j] < nums[k].

The value of this triplet is calculated as: nums[i] - nums[j] + nums[k]

Example 1:

Input: nums = [5,6,9]  

Output: 8

Explanation: The only valid increasing triplet is (0,1,2) with values [5,6,9].

The value is 5 - 6 + 9 = 8.

Example 2:

Input: nums = [1,5,3,6]  

Output: 4

Explanation:

Valid increasing triplets:

(0,1,3): 1 - 5 + 6 = 2

(0,2,3): 1 - 3 + 6 = 4 (Maximum value)

Now Your Turn!

Pick the correct output for the given input

Input: nums = [2, 7, 4, 9, 5]  

Still unsure what the problem is asking ?

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

Constraints:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • At least one triplet meets the condition.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maximumTripletValue(vector<int>& nums) {
}
};
 
Test Case

Input:

Nums