• + 0 comments

    This is failing for few test cases due to timeout. Need to optimize it.

    def minimumBribes(q):
        total = index = 0
        r = list(range(1, len(q)+1))
        while index < len(q):
            jump = abs(index - r.index(q[index]))
            if jump > 2:
                print("Too chaotic")
                return
            if jump == 1:
                r[index], r[index+jump] = r[index+jump], r[index]
            elif jump == 2:
                temp = r[index+jump]
                r[index+jump], r[index+jump-1] = r[index+jump-1], r[index]
                r[index] = temp
            total += jump
            index += 1
        print(total)