We have given an array of size $N$, consisting of positive integers, and an integer $H$. We also know that an array is stable iff $|A_i - A_{i-1}| \leq H$ for each $1<i<N-1$. We can modify the element by adding +1 to some element or by adding -1 to some element, we don't have a limit on how much we can use those operations, but we want to make the array stable in minimum moves.
I started solving the task by writing a dynamic programming relation, described by the state $f(i, j)$ meaning the minimum moves to stabilize the first $i$ elements of the array, such that the last element is equal to $j$.
However it seems that this method is slow for big values of $N$. How would you approach solving this problem?