educative.io

Educative

Algorithm correctness

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}

Yes for me as well, attempted code separately, the answer comes to 3.