• + 0 comments

    No need for DP. Key insight is that adding to all colleagues except one is equivalent to taking away from only one colleague, with the modification of not "bottoming out" in cases where equality can be reached in fewer moves with bigger additions. For example [1, 5, 5] can reach equality in 4 add-2 moves at [9, 9, 9] ([1, 1, 1] if you take-away), but in only 3 total moves at [11, 11, 11] ([0, 0, 0] if you take-away). This is the reason for my offset param. The main logic gets the distance between each colleague and the minimum, and sums the multiples of 5, 2, and 1 necessary to equalize all colleagues at the minimum.

    def equal(arr):
        def equalize(arr, offset):
            n_rounds = 0
            least = min(arr) - offset
            gaps = [x - least for x in arr]
            for choc in (5, 2, 1):
                n_rounds += sum(x // choc for x in gaps)
                gaps = [x % choc for x in gaps]
            return n_rounds
        return min(equalize(arr, x) for x in range(4))