-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum_bribes.rb
More file actions
48 lines (33 loc) · 989 Bytes
/
minimum_bribes.rb
File metadata and controls
48 lines (33 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def minimumBribes(q)
# Write your code here
i = q.length - 1
localBribe = 0
while i >= 0
if q[i] != (i + 1)
if ((i - 1) >= 0) && (q[i - 1] == (i + 1))
temp = q[i - 1]
q[i - 1] = q[i]
q[i] = temp
localBribe = localBribe + 1
elsif ((i - 2) >= 0) && (q[i - 2] == (i + 1))
temp = q[i - 2]
q[i - 2] = q[i - 1]
q[i -1] = q[i]
q[i] = temp
localBribe = localBribe + 2
else
puts 'Too chaotic'
return
end
end
i = i - 1
end
if localBribe > 0
puts localBribe
end
end
inputArray = [2, 1, 5, 3, 4]
inputArrayTwo = [1, 2, 5, 3, 7, 8, 6, 4]
#0, 1, 2, 3, 4, 5, 6, 7
puts minimumBribes(inputArray)
puts minimumBribes(inputArrayTwo)