New Year Chaos

  • + 0 comments

    This doesn't fully work

    def minimumBribes(q):    
        # Write your code here
        # q is an arr
        #q = q[::-1]
        sorted_q = sorted(q)
        total_count = 0
        def find_diff(q, target, idx):
            # this finds whats the idx of target in arr q
            # returns the diff of that and idx, if q < idx, ignore
            # if not, add diff to total count
            return q.index(target) - idx
            
        for i in range(len(q) - 1, -1, -1):
            if sorted_q[i] == q[i]:
                continue
            else:
                diff = -find_diff(q, sorted_q[i], i)
                if diff > 2:
                    print("Too chaotic")
                    return
                elif diff > 0:
                    total_count += diff
        print(total_count)