Ctrl K

Steep Subsequence With the Largest Sum

Hard
You get an integer array values. Pick a non-empty subsequence at indices i₀ < i₁ < … < iₘ₋₁. It is steep if each picked element exceeds the previous one by at least the distance between them: values[iⱼ] - values[iⱼ₋₁] ≥ iⱼ - iⱼ₋₁ for every j from 1 to m - 1. A single element is always steep. Return the largest possible sum of a steep subsequence.
Example 1
values=[1, 4, 2, 8] → 13

Take indices 0, 1, 3: 4 - 1 ≥ 1 - 0 and 8 - 4 ≥ 3 - 1. The sum is 1 + 4 + 8 = 13.

Example 2
values=[-5, -2, -9] → -2

All numbers are negative, so the best choice is the single number -2.

Example 3
values=[3, 2, 1, 7] → 10

3 and 7 differ by 4 at distance 3, which is fine, giving 10. Pairs like 3, 2 or 2, 1 do not qualify.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardCost of Filling a Sorted Shelf HardTriples Both Rankings Agree On Hard
loading Python…
Input
values=[1, 4, 2, 8]
Expected
13

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