Alex is given n
piles of boxes of equal or unequal heights. In one step, Alex can remove any number of boxes from the pile which has the maximum height and try to make it equal to the one which is just lower than the maximum height of the stack. Determine the minimum number of steps required to make all of the piles equal in height.
Function Description
Complete the function pilesOfBoxes
in the editor.
pilesOfBoxes
has the following parameter(s):
int boxesInPiles[n]
: eachboxesInPiles[i]
represents the initial height of one pile
Returns
long
: the minimum number of steps required
Example 1:
Input: boxesInPiles = [5, 2, 1]
Output: 3
Explanation:![]()
Initial State Step 1 Step 2 Step 3
In the first step, remove 3 boxes from
boxesInPiles[0]
, and the new array isboxesInPiles' = [2, 2, 1]
. Now reduce the two taller piles by 1 box each to match the height of the shortest pile. This takes 2 steps because each step is performed on only one pile. The final number of steps required is 3.
๐
๐

input:
output: