New Year Chaos

  • + 0 comments

    Python solution:

    def minimumBribes(arr: list) -> None:
        """Count bribes from the perspective of the person being overtaken."""
        bribes = 0
        for i, sticker in enumerate(arr): 
            pos = i + 1     # queue position is 1-indexed
            # check for 2+ bribes:
            if sticker - pos > 2:
                print("Too chaotic")
                return
            # for each person, count no. of overtakes
            # only have to check previous two positions:
            for j in range(max(0, sticker - 2), i):    # not i - 2
                if arr[j] > sticker:
                    bribes += 1                    
        print(bribes)