Min Steps to Make Piles Equal Height

Given N piles of equal or unequal heights. In one step, You 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.

Example 1:

Input: [5, 2, 1]

Output: 3

Explanation:

Step 1: reducing 5 -> 2 = [2, 2, 1] Step 2: reducing 2 -> 1 = [2, 1, 1] Step 3: reducing 2 -> 1 = [1, 1, 1]

Try it yourself

Implementation

Expand 1 lines ...
2
2
from collections import Counter
3
3
4
4
def minSteps(nums: List[int]) -> int:
5
-
    # WRITE YOUR BRILLIANT CODE HERE
5
+
    cnt = Counter(nums)
6
+
    nums = sorted(cnt.keys(), reverse=True)
7
+
    k, ans = 0, 0
8
+
    for x in nums[:-1]:
9
+
        k += cnt[x]
10
+
        ans += k
11
+
    return ans
12
+
6
13
if __name__ == "__main__":
7
14
    arr = [int(x) for x in input().split()]
8
15
    print(minSteps(arr))