i-th tower has height heights[i]. One move goes like this:
1. Find the tallest tower; if there are several, take the leftmost one.
2. Find the next height down among all towers, that is, the largest value strictly below the chosen tower's height.
3. Take blocks off the chosen tower until it has that height.
Moves repeat until all towers have the same height. Return how many moves that takes.heights=[7, 2, 7, 4] → 5
The distinct heights are 2, 4 and 7. Each tower of height 7 is lowered twice (to 4, then to 2), the tower of height 4 once, and the 2 stays put. That is 2 + 2 + 1 = 5 moves.
heights=[3, 3] → 0
The towers already match, so no moves are needed.
heights=[1, 5, 5, 2, 9] → 8
heights=[7, 2, 7, 4]
5
Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.