The Fibonacci sequence is defined using the following recursive formula:
F(0) = 0 F(1) = 1 F(M) = F(M - 1) + F(M - 2) if M >= 2A small frog wants to get to the other side of a river. The frog is initially located at one bank of the river (position −1) and wants to get to the other bank (position N). The frog can jump over any distance F(K), where F(K) is the K-th Fibonacci number. Luckily, there are many leaves on the river, and the frog can jump between the leaves, but only in the direction of the bank at position N.
The leaves on the river are represented in an array A consisting of N integers. Consecutive elements of array A represent consecutive positions from 0 to N − 1 on the river. Array A contains only 0s and/or 1s:
- 0 represents a position without a leaf;
- 1 represents a position containing a leaf.
The goal is to count the minimum number of jumps in which the frog can get to the other side of the river (from position −1 to position N). The frog can jump between positions −1 and N (the banks of the river) and every position containing a leaf.
For example, consider array A such that:
A[0] = 0 A[1] = 0 A[2] = 0 A[3] = 1 A[4] = 1 A[5] = 0 A[6] = 1 A[7] = 0 A[8] = 0 A[9] = 0 A[10] = 0The frog can make three jumps of length F(5) = 5, F(3) = 2 and F(5) = 5.
Write a function:
def solution(A)
that, given an array A consisting of N integers, returns the minimum number of jumps by which the frog can get to the other side of the river. If the frog cannot reach the other side of the river, the function should return −1.
For example, given:
A[0] = 0 A[1] = 0 A[2] = 0 A[3] = 1 A[4] = 1 A[5] = 0 A[6] = 1 A[7] = 0 A[8] = 0 A[9] = 0 A[10] = 0the function should return 3, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer that can have one of the following values: 0, 1.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 13:Fibonacci numbers
# P 13.1 FibFrog
def fib(n):
"""
构造不大于n的斐波那契数的列表
:param n: 最大值
:return: 斐波那契数列[1, 1, 2, 3, 5, 8 ……]
"""
fib_list = [1]
a = fib_list[0]
b = 1
while b <= n:
fib_list.append(b)
a, b = b, a+b
return fib_list
def solution(A):
"""
判断能否按照斐波那契步数过河。 时间复杂度O(N * log(N))
:param A: 表示河面上树叶情况的数组
:return: 能,返回最少次数。不能,返回-1
"""
A = [1] + A + [1] # 添加开始的位置-1,以及结束的位置N
length = len(A)
fib_list = fib(length)
if length - 1 == fib_list[-1]: # 一次就可从位置-1跳到位置N
return 1
else:
sign_list = [length] * length # 参照序列
sign_list[0] = 0
for i in range(1, length):
if A[i] == 1: # 此处有树叶
# 遍历斐波那契数列, 寻找最少的跳跃次数
for j in fib_list:
dis = i - j # dis不得小于0,
if dis >= 0:
if sign_list[dis] + 1 < sign_list[i]: # 说明dis位置可以斐波那契到达的,
sign_list[i] = sign_list[dis] + 1 # 达到位置dis的次数再加上走长度为斐波那契数j的一次
print(sign_list)
else:
break # 后面的斐波那契数更大
if sign_list[-1] < length:
return sign_list[-1]
else:
return -1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 13:Fibonacci numbers
# P 13.1 FibFrog
def fib(n):
"""
构造不大于n的斐波那契数的列表
:param n: 最大值
:return: 斐波那契数列[1, 1, 2, 3, 5, 8 ……]
"""
fib_list = [1]
a = fib_list[0]
b = 1
while b <= n:
fib_list.append(b)
a, b = b, a+b
return fib_list
def solution(A):
"""
判断能否按照斐波那契步数过河。 时间复杂度O(N * log(N))
:param A: 表示河面上树叶情况的数组
:return: 能,返回最少次数。不能,返回-1
"""
A = [1] + A + [1] # 添加开始的位置-1,以及结束的位置N
length = len(A)
fib_list = fib(length)
if length - 1 == fib_list[-1]: # 一次就可从位置-1跳到位置N
return 1
else:
sign_list = [length] * length # 参照序列
sign_list[0] = 0
for i in range(1, length):
if A[i] == 1: # 此处有树叶
# 遍历斐波那契数列, 寻找最少的跳跃次数
for j in fib_list:
dis = i - j # dis不得小于0,
if dis >= 0:
if sign_list[dis] + 1 < sign_list[i]: # 说明dis位置可以斐波那契到达的,
sign_list[i] = sign_list[dis] + 1 # 达到位置dis的次数再加上走长度为斐波那契数j的一次
print(sign_list)
else:
break # 后面的斐波那契数更大
if sign_list[-1] < length:
return sign_list[-1]
else:
return -1
[0, 13, 13, 13, 13, 1, 13, 13, 13, 13, 13, 13, 13] [0, 13, 13, 13, 13, 1, 13, 2, 13, 13, 13, 13, 13] [0, 13, 13, 13, 13, 1, 13, 2, 13, 13, 13, 13, 3]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 13:Fibonacci numbers
# P 13.1 FibFrog
def fib(n):
"""
构造不大于n的斐波那契数的列表
:param n: 最大值
:return: 斐波那契数列[1, 1, 2, 3, 5, 8 ……]
"""
fib_list = [1]
a = fib_list[0]
b = 1
while b <= n:
fib_list.append(b)
a, b = b, a+b
return fib_list
def solution(A):
"""
判断能否按照斐波那契步数过河。 时间复杂度O(N * log(N))
:param A: 表示河面上树叶情况的数组
:return: 能,返回最少次数。不能,返回-1
"""
A = [1] + A + [1] # 添加开始的位置-1,以及结束的位置N
length = len(A)
fib_list = fib(length)
if length - 1 == fib_list[-1]: # 一次就可从位置-1跳到位置N
return 1
else:
sign_list = [length] * length # 参照序列
sign_list[0] = 0
for i in range(1, length):
if A[i] == 1: # 此处有树叶
# 遍历斐波那契数列, 寻找最少的跳跃次数
for j in fib_list:
dis = i - j # dis不得小于0,
if dis >= 0:
if sign_list[dis] + 1 < sign_list[i]: # 说明dis位置可以斐波那契到达的,
sign_list[i] = sign_list[dis] + 1 # 达到位置dis的次数再加上走长度为斐波那契数j的一次
else:
break # 后面的斐波那契数更大
if sign_list[-1] < length:
return sign_list[-1]
else:
return -1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 13:Fibonacci numbers
# P 13.1 FibFrog
def fib(n):
"""
构造不大于n的斐波那契数的列表
:param n: 最大值
:return: 斐波那契数列[1, 1, 2, 3, 5, 8 ……]
"""
fib_list = [1]
a = fib_list[0]
b = 1
while b <= n:
fib_list.append(b)
a, b = b, a+b
return fib_list
def solution(A):
"""
判断能否按照斐波那契步数过河。 时间复杂度O(N * log(N))
:param A: 表示河面上树叶情况的数组
:return: 能,返回最少次数。不能,返回-1
"""
A = [1] + A + [1] # 添加开始的位置-1,以及结束的位置N
length = len(A)
fib_list = fib(length)
if length - 1 == fib_list[-1]: # 一次就可从位置-1跳到位置N
return 1
else:
sign_list = [length] * length # 参照序列
sign_list[0] = 0
for i in range(1, length):
if A[i] == 1: # 此处有树叶
# 遍历斐波那契数列, 寻找最少的跳跃次数
for j in fib_list:
dis = i - j # dis不得小于0,
if dis >= 0:
if sign_list[dis] + 1 < sign_list[i]: # 说明dis位置可以斐波那契到达的,
sign_list[i] = sign_list[dis] + 1 # 达到位置dis的次数再加上走长度为斐波那契数j的一次
else:
break # 后面的斐波那契数更大
if sign_list[-1] < length:
return sign_list[-1]
else:
return -1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# -*- coding:utf-8 -*-
# &Author AnFany
# Lesson 13:Fibonacci numbers
# P 13.1 FibFrog
def fib(n):
"""
构造不大于n的斐波那契数的列表
:param n: 最大值
:return: 斐波那契数列[1, 1, 2, 3, 5, 8 ……]
"""
fib_list = [1]
a = fib_list[0]
b = 1
while b <= n:
fib_list.append(b)
a, b = b, a+b
return fib_list
def solution(A):
"""
判断能否按照斐波那契步数过河。 时间复杂度O(N * log(N))
:param A: 表示河面上树叶情况的数组
:return: 能,返回最少次数。不能,返回-1
"""
A = [1] + A + [1] # 添加开始的位置-1,以及结束的位置N
length = len(A)
fib_list = fib(length)
if length - 1 == fib_list[-1]: # 一次就可从位置-1跳到位置N
return 1
else:
sign_list = [length] * length # 参照序列
sign_list[0] = 0
for i in range(1, length):
if A[i] == 1: # 此处有树叶
# 遍历斐波那契数列, 寻找最少的跳跃次数
for j in fib_list:
dis = i - j # dis不得小于0,
if dis >= 0:
if sign_list[dis] + 1 < sign_list[i]: # 说明dis位置可以斐波那契到达的,
sign_list[i] = sign_list[dis] + 1 # 达到位置dis的次数再加上走长度为斐波那契数j的一次
else:
break # 后面的斐波那契数更大
if sign_list[-1] < length:
return sign_list[-1]
else:
return -1
The solution obtained perfect score.