Skip to content

Instantly share code, notes, and snippets.

@namthatman
Last active April 20, 2019 05:55
Show Gist options
  • Save namthatman/75a151fa4414eaa24fa7f0f86288c11b to your computer and use it in GitHub Desktop.
Save namthatman/75a151fa4414eaa24fa7f0f86288c11b to your computer and use it in GitHub Desktop.
New Year Chaos
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the minimumBribes function below.
def minimumBribes(q):
bribesTime = 0
invalid = False
for index in range(len(q)):
if abs(q[index]-index-1) > 2:
print('Too chaotic')
invalid = True
break
else:
bribesTime += abs(q[index]-index-1)
if invalid == False:
print(int(bribesTime/2))
if __name__ == '__main__':
t = int(input())
for t_itr in range(t):
n = int(input())
q = list(map(int, input().rstrip().split()))
minimumBribes(q)
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by 1 from 1 at the front of the line to n at the back.
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if n = 8 and Person 5 bribes Person 4, the queue will look like this: 1,2,3,5,4,6,7,8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Function Description
Complete the function minimumBribes in the editor below. It must print an integer representing the minimum number of bribes necessary, or Too chaotic if the line configuration is not possible.
minimumBribes has the following parameter(s):
q: an array of integers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment