Categories
coding solutions

Codility: FibFrog Solution

Task

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.

Write a function: int solution(vector<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.

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.

Solution

The solution uses a one-dimensional version of Dijkstra’s algorithm.
The item jumps[n] contains the provisional minimum number of jumps needed to reach the leaf at position n, and the number of leaves with known distances (in jumps) from the starting position can be increased by 1 each time.
#include <cassert> #include <iostream> #include <math.h> #include <vector> using namespace std; void calcFibs(vector<int> &Fibs, int N) { // skip 0,1 Fibs.push_back(1); Fibs.push_back(2); int next = 3; int i = 3; while (next <= N + 1) { Fibs.push_back(next); next = Fibs[i – 2] + Fibs[i – 1]; i++; } } int solution(vector<int> &A) { vector<int> Fibs; int N = A.size(); calcFibs(Fibs, N); // min number of jumps needed to reach psn vector<int> jumps(N + 1); for (int i = 0; i <= N; i++) jumps[i] = INT32_MAX; // valid values will always be <=N<INT32_MAX bc 1 is a Fib number // initial jump for (int f : Fibs) if (f <= N + 1) jumps[f – 1] = 1; // 1D dijkstra’s for (int start = 0; start < N; start++) { if (A[start] == 1) { int minJumps = jumps[start]; if (minJumps != INT32_MAX) { for (int j : Fibs) { if (start + j > N) break; if ((start + j == N) || (A[start + j] == 1)) { jumps[start + j] = min(jumps[start + j], minJumps + 1); } } } } } return jumps[N] == INT32_MAX ? -1 : jumps[N]; }

Test Function

This code tests the solution

void check(vector<int> P, int expected) { int res = solution(P); assert(res == expected); } int main() { vector<int> Fibs; calcFibs(Fibs, 100000); assert(Fibs[23] == 75025); assert(Fibs[22] == 46368); // 1 1 2 3 5 8 13 21 check({0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0}, 3); check({0, 0, 0}, -1); check({0, 0, 0, 0}, 1); check({1, 0, 0, 0, 0}, 2); check({0, 1, 0, 1, 0}, 3); }

Results

Codility Results

Correctness100%
Performance100%
Time Complexity O(N * log(N))

Other Solutions

These blogs also have solutions:

Index of solutions to Codility Lessons in C++

Leave a Reply

Your email address will not be published. Required fields are marked *