i has height heights[i]. In one step you can raise or lower any single plot by exactly 1.
The row of heights must become monotonic: either non-decreasing (each plot is no lower than the one before it) or non-increasing (each plot is no higher). You choose the direction.
Return the minimum number of steps.heights=[4, 1, 3, 2] → 2
Raise the second plot from 1 to 2 and lower the third from 3 to 2: the row becomes [4, 2, 2, 2], which never goes up. Two steps in total.
heights=[2, 6, 3, 8] → 3
Lower 6 to 3: [2, 3, 3, 8] never goes down. That takes 3 steps, and no way is shorter.
heights=[9, 9, 5, 0] → 0
The heights already never go up, so nothing needs to change.
heights) ≤ 1000heights[i] ≤ 1000heights=[4, 1, 3, 2]
2
Run checks the first 3 tests in your browser. Submit checks all 138 tests on the server, hidden ones included.