Please read the new rule regarding the restriction on the use of AI tools. ×

Make array non-decreasing or non-increasing

Revision en1, by neo_30, 2023-10-20 01:52:03

Given an 0-index-based array. You have to make the array non-increasing or non-decreasing. The operation you can use:

  1. Choose an index $$$0\leq i<n$$$.
  2. 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.

Tags array

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English neo_30 2023-10-20 01:52:03 549 Initial revision (published)