educative.io

Algorithm correctness


#1

When I look at the given test data heights={2, 1, 2, 5, 1}, I could collect all the coins in 3 steps, 2 horizontal and one vertical.
Why does the algorithm return 4?
And how comes, it returns 3 after swapping the first two values: {1, 2, 2, 5, 1}