Codility经典算法题之三十三:FibFrog

本文探讨了一只青蛙如何利用斐波那契数列跳跃过河的问题。青蛙从河的一边出发,目标到达另一边,它只能跳到有叶子的位置上,并且每次跳跃的距离必须是斐波那契数列中的一个数。文章提供了一个解决方案,通过计算最小跳跃次数来帮助青蛙成功过河。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Task Description:

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 >= 2

A 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] = 0

The frog can make three jumps of length F(5) = 5, F(3) = 2 and F(5) = 5.

Write a function:

class Solution { public int solution(int[] 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] = 0

the function should return 3, as explained above.

Assume that:

  • 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.

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Solution:

def fibonacci(n):
    fib = [1,1]
    while fib[-1] < n:
        fib.append(fib[-1] + fib[-2])
    return fib[1:-1]


def solution(A):
    jumps = fibonacci(len(A) + 2)


    A.insert(0,1)#insert 1 into the first place
    A.append(1)  #insert 1 into the last  place
    lenA = len(A)


    reach = [0] * lenA


    for pos in range(1,lenA):
        jumpmin = lenA
        for jump in jumps: #[1, 2, 3, 5, 8]
            leftsteps = pos - jump
            if leftsteps >= 0:
                if A[leftsteps] == 1 and reach[leftsteps] + 1 < jumpmin:
                    jumpmin = reach[leftsteps] + 1
            else:
                break


            reach[pos] = jumpmin


    return reach[-1] if reach[-1] != lenA else -1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值