Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/ruby
- require 'json'
- require 'stringio'
- def minimumBribes(q)
- count = 0
- number_of_swaps = []
- while(!ordered?(q))
- i = first_unordered_index(q)
- number_of_swaps[q[i]] = (number_of_swaps[q[i]] || 0) + 1
- return 'Too chaotic' if number_of_swaps[q[i]] > 2
- swap(q, i, i + 1)
- count += 1
- end
- return count
- end
- def ordered?(q)
- q.sort == q
- end
- def first_unordered_index(q)
- i = 0
- while(i < q.size - 1 && q[i]<q[i+1])
- i+=1
- end
- i
- end
- def swap(array, i, j)
- temp = array[i]
- array[i] = array[j]
- array[j] = temp
- end
- t = gets.to_i
- t.times do |t_itr|
- n = gets.to_i
- q = gets.rstrip.split(' ').map(&:to_i)
- puts minimumBribes q
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement