Ctrl K

Steady Climb: Best Pick of Days

Medium
You get an array prices, where prices[i] is a stock's price on day i (days are 0-based). Pick some days, at least one, so that between any two consecutive picked days the price grew by exactly as much as the number of days that passed. In other words, for consecutive picked days i < j it must hold that prices[j] - prices[i] == j - i. The score of a pick is the sum of the prices on the picked days. Return the highest score possible. A single picked day is a valid pick too.
Example 1
prices=[3, 9, 5, 6, 10, 8] → 22

Pick days 0, 2, 3 and 5 with prices 3, 5, 6, 8. From day 0 to day 2 the price rose by 2 over 2 days, from 2 to 3 by 1 over 1 day, from 3 to 5 by 2 over 2 days. The total is 22.

Example 2
prices=[7, 4, 1] → 7

The price only falls, so no two days can go together. The best is the single day priced 7.

Example 3
prices=[2, 3, 4, 20, 6] → 20
Constraints
  • 1 ≤ len(prices) ≤ 10⁵
  • 1 ≤ prices[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
prices=[3, 9, 5, 6, 10, 8]
Expected
22

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