New Year Chaos

  • + 0 comments

    Python3: another O(N) (??? maybe ???)

    def minimumBribes(q):
        seen = set()
        bribes = {n: set() for n in range(0, len(q) + 1)}
        for n in q:
            seen.add(n)
            m = n - 1
            while m > 0 and m not in seen:
                bribes[n].add(m)
                m -= 1
            bribes[n] |= (bribes[m] - seen)
            if len(bribes[n]) > 2:
                return print('Too chaotic')
            
        print(sum(len(v) for v in bribes.values()))