Microsoft Online Assessment (OA) - 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

1from typing import Counter, List
2
3def min_steps(nums: List[int]) -> int:
4    cnt = Counter(nums)
5    nums = sorted(cnt.keys(), reverse=True)
6    k, ans = 0, 0
7    for x in nums[:-1]:
8        k += cnt[x]
9        ans += k
10    return ans
11
12if __name__ == "__main__":
13    nums = [int(x) for x in input().split()]
14    res = min_steps(nums)
15    print(res)
16
1import java.util.Arrays;
2import java.util.List;
3import java.util.Scanner;
4import java.util.stream.Collectors;
5
6class Solution {
7    public static int minSteps(List<Integer> nums) {
8        int numsSize = nums.size();
9
10        if (numsSize < 2) {
11            return 0;
12        }
13
14        nums.sort(null);
15        int sum = 0;
16
17        for (int i = 1; i < numsSize; ++i) {
18            if (nums.get(numsSize - i - 1) != nums.get(numsSize - i)) {
19                sum += i;
20            }
21        }
22
23        return sum;
24    }
25
26    public static List<String> splitWords(String s) {
27        return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
28    }
29
30    public static void main(String[] args) {
31        Scanner scanner = new Scanner(System.in);
32        List<Integer> nums = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
33        scanner.close();
34        int res = minSteps(nums);
35        System.out.println(res);
36    }
37}
38
1"use strict";
2
3function minSteps(nums) {
4    const numsSize = nums.length;
5
6    if (numsSize < 2) {
7        return 0;
8    }
9    nums.sort();
10    let sum = 0;
11
12    for (let i = 1; i < numsSize; ++i) {
13        if (nums[numsSize - i - 1] !== nums[numsSize - i]) {
14            sum += i;
15        }
16    }
17    return sum;
18}
19
20function splitWords(s) {
21    return s === "" ? [] : s.split(" ");
22}
23
24function* main() {
25    const nums = splitWords(yield).map((v) => parseInt(v));
26    const res = minSteps(nums);
27    console.log(res);
28}
29
30class EOFError extends Error {}
31{
32    const gen = main();
33    const next = (line) => gen.next(line).done && process.exit();
34    let buf = "";
35    next();
36    process.stdin.setEncoding("utf8");
37    process.stdin.on("data", (data) => {
38        const lines = (buf + data).split("\n");
39        buf = lines.pop();
40        lines.forEach(next);
41    });
42    process.stdin.on("end", () => {
43        buf && next(buf);
44        gen.throw(new EOFError());
45    });
46}
47
Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro
Favorite (idle)