Min Steps Equal Piles
MediumSorting · Microsoft
You have piles of stones with heights piles. In one step you pick one pile of the current maximum height and lower it to the next smaller height that currently exists among the piles (the largest height strictly below it).
Return the number of steps needed until every pile has the same height.
Example 1
- Input:
- piles = [3, 1, 3, 2]
- Output:
- 5
- Explanation
- Each pile of 3 drops to 2, taking 2 steps. That leaves three piles of 2, and each drops to 1, taking 3 more steps, for 5 in total.
Example 2
- Input:
- piles = [4, 4, 4]
- Output:
- 0
- Explanation
- All the piles already have the same height.
Example 3
- Input:
- piles = [6, 2]
- Output:
- 1
- Explanation
- The only lower height is 2, so the pile of 6 drops straight to it in one step.
Constraints
1 <= piles.length <= 5 * 10^41 <= piles[i] <= 5 * 10^4