有人可以指出我逻辑中的错误。 (编码练习)

Could someone point out the error in my logic. (Coding Exercise)

所以这是来自网站 HackerRank 的问题。我已经尽力了,但我终生无法弄清楚自己做错了什么。题目如下:

“今天是元旦,人们正在排队乘坐仙境过山车。每个人都贴着一张贴纸,表明他们在 1n 队列中的初始位置。任何人都可以贿赂前面的人调换位置,人家还是贴着原来的贴纸,一个人最多贿赂两个人

确定达到给定队列顺序所发生的最少贿赂次数。打印贿赂的数量,或者,如果有人贿赂了两个以上的人,则打印 Too chaotic."

这是我的尝试:

def minimumBribes(q):
    bribes = 0
    sorted_q  = sorted(q)
    total_moves = 0
    for i in range(len(q)):
        origin = sorted_q[i]
        current = q[i]
        moved = current - origin
        if moved > 0:
            if moved > 2:
                print("Too chaotic")
                return
            else:
                total_moves += moved
    print(total_moves)

我知道这有点罗嗦,但我想让我的逻辑尽可能清晰。

如果我输入 q = [1,2,5,3,7,8,6,4] 预期输出是 7 但我得到 6

为什么!

q=[1,2,5,3,7,8,6,4]

正如您在问题中所看到的那样,没有提及贿赂顺序 1 可以贿赂 2 一次人或一次 1 人

没有被人贿赂就不能贿赂的限制

让我们反转打洞过程,像“q”中那样创造这种情况

'''

   sorted_q=[1,2,3,4,5,6,7,8]
   5 bribed 3 and 4 
   total_moved = 2
   
   sorted_q=[1,2,5,3,4,6,7,8]
   7 bribed 4 and 6 
   total_moved = 4
    
   sorted_q=[1,2,5,3,7,4,6,8]
   8 bribed 4 and 6
   total_moved = 6

   sorted_q=[1,2,5,3,7,8,4,6]
   here at this bribe by 6 to 4 just make your logic fail
   total_moved = 7
   

   sorted_q=[1,2,5,3,7,8,6,4]
    

'''