forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibFrog.py
More file actions
46 lines (33 loc) · 979 Bytes
/
FibFrog.py
File metadata and controls
46 lines (33 loc) · 979 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
# link: https://codility.com/demo/take-sample-test/fib_frog
# name: FibFrog
__author__ = 'mislav'
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
# write your code in Python 2.7
max_size = 100001
n = len(A)
fibs = [0, 1]
while fibs[-1] < n + 1:
fibs.append(fibs[-1] + fibs[-2])
A.append(1)
result = [max_size]*(n+1)
for index in xrange(n+1):
if A[index] == 0:
continue
for fib in fibs:
if index - fib < -1:
break
prev = index-fib
if prev == -1:
result[index] = 1
break
if result[prev] != max_size:
result[index] = min(result[index], result[prev]+1)
if result[-1] == max_size:
return -1
return result[-1]
print solution([0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0])
print solution([0, 0])
print solution([1, 1])
print solution([])