Problem · Array
Minimum Pile Reduction Steps
Learn this problemProblem statement
You are given a nonempty array heights, where each value is the height of one pile.
In one step, choose one pile whose height is currently maximal and lower that pile to the greatest strictly smaller height currently present among the piles.
Return the minimum number of steps needed to make every pile have the same height.
Function
minimumPileReductionSteps(heights: int[]) → longExamples
Example 1
heights = [5,2,1]return = 3Lower 5 to 2 in one step, then lower each of the two piles of height 2 to 1. The total is 3 steps.
Example 2
heights = [4,4,4]return = 0All piles already have the same height.
Example 3
heights = [10,10,5,5,1]return = 6The two piles of height 10 take two steps to reach 5. The four piles then at height 5 take four more steps to reach 1.
Constraints
1 <= heights.length <= 1000001 <= heights[i] <= 1000000000- The answer fits in a signed 64-bit integer.