Problem · Array

Minimum Pile Reduction Steps

Learn this problem
EasyAmerican Express logoAmerican ExpressFULLTIMEOA

Problem 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[]) → long

Examples

Example 1

heights = [5,2,1]return = 3

Lower 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 = 0

All piles already have the same height.

Example 3

heights = [10,10,5,5,1]return = 6

The 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 <= 100000
  • 1 <= heights[i] <= 1000000000
  • The answer fits in a signed 64-bit integer.

More American Express problems

drafts saved locally
public long minimumPileReductionSteps(int[] heights) {
    // Write your code here.
}
heights[5,2,1]
expected3
checking account