Ctrl K

Best Rising Triple

Medium
You get an integer array values. A rising triple is a set of indices i < j < k with values[i] < values[j] < values[k]. Its worth is values[i] - values[j] + values[k]. Return the largest worth over all rising triples. The input is guaranteed to contain at least one rising triple.
Example 1
values=[4, 7, 10] → 7

The only rising triple is the whole array: 4 − 7 + 10 = 7.

Example 2
values=[2, 8, 6, 3, 9] → 8

The best pick is 2, 3, 9: 2 − 3 + 9 = 8. For comparison, 2, 6, 9 gives only 5.

Example 3
values=[5, 1, 6, 2, 7, 3] → 6
Constraints
  • 3 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
  • At least one rising triple exists
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLow, Peak, Middle MediumKitchen Order Board MediumLongest Tight-Range Stretch MediumOne Edit to the Closest Match MediumSearch Tree Height From Insertion Order Medium
loading Python…
Input
values=[4, 7, 10]
Expected
7

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