Problem · Array

Minimum Cost to Make an Array Monotonic

Learn this problem
HardAtlassian logoAtlassianFULLTIMEOA

Problem statement

Replace array values by integers at cost |newValue - originalValue|. Return the minimum total cost needed to make the array non-decreasing or non-increasing.

Function

minMonotonicCost(arr: int[]) → long

Examples

Example 1

arr = [1,5,2,4]return = 3

Changing 5 to 2 yields the non-decreasing array [1,2,2,4] at cost 3.

Constraints

  • 1 <= arr.length <= 2000
  • -10^6 <= arr[i] <= 10^6
  • The answer fits in a signed 64-bit integer.

More Atlassian problems

drafts saved locally
public long minMonotonicCost(int[] arr) {
  // Write your code here.
}
arr[1,5,2,4]
expected3
checking account