Given an 0-index-based array. You have to make the array non-increasing or non-decreasing. The operation you can use:
- Choose an index $$$0\leq i<n$$$.
- Make $$$array[i] = array[i]-1$$$ or $$$array[i] = array[i]+1$$$.
Find the minimum operations required to achieve the target.
- $$$Array.size()\leq1000$$$
- $$$0\leq Array[i] \leq1000$$$
Sample: $$$array = [3,2,4,5,0]$$$
$$$ans = 4$$$
final $$$array = [3,3,3,3,0]$$$
Please help me understand how to reach an optimal solution.