Skip to content

Instantly share code, notes, and snippets.

@hopewise
Created October 27, 2019 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopewise/7c349a89238aacf3fd0df0129ae263f1 to your computer and use it in GitHub Desktop.
Save hopewise/7c349a89238aacf3fd0df0129ae263f1 to your computer and use it in GitHub Desktop.
New Year Chaos, I don't know why it fails at case: `1 2 5 3 7 8 6 4` ?
#!/bin/ruby
require 'json'
require 'stringio'
# Complete the minimumBribes function below.
def minimumBribes(q)
too = false
bs = 0
(1..q.count).each do |i|
a = q[i-1]
brides = a - i
#p "#{a} at #{i} is #{brides}"
if brides > 2
bs = 0
puts "Too chaotic"
break
else
bs = bs + brides if brides > 0
end
end
puts (bs) if bs > 0
end
t = gets.to_i
t.times do |t_itr|
n = gets.to_i
q = gets.rstrip.split(' ').map(&:to_i)
minimumBribes q
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment