Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Make array non-decreasing or non-increasing

Правка en1, от 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.

Теги array

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский neo_30 2023-10-20 01:52:03 549 Initial revision (published)