Minimum Increase to Maximize Special Indices

You are given an integer array nums of length n.

An index i where 0 < i < n - 1 is special if nums[i] > nums[i - 1] and nums[i] > nums[i + 1].

You may perform operations where you choose any index i and increase nums[i] by 1.

Your goal is to:

  • Maximize the number of special indices.
  • Minimize the total number of operations required to achieve that maximum.

Return an integer denoting the minimum total number of operations required.

Example 1
Inputnums = [1,2,2]
Output1
Increasing nums[1] once gives [1, 3, 2], which has 1 special index, the maximum achievable, with the fewest operations.
Example 2
Inputnums = [2,1,1,3]
Output2
Performing 2 operations at index 1 gives [2, 3, 1, 3], which has 1 special index, the maximum achievable.

Constraints

  • 3 <= n <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate