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:
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.
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 use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <algorithm>
#include <queue>
/**
* Generates the list of fib numbers from 2-th fib until n (inclusive n if n is also fib) in reversed order
*
* For example, the fib number until 10 are: 0, 1, 1, 2, 3, 5, 8
*
* fibonaccies_until(10) results: [1, 2, 3, 5, 8]
*
* and
*
* fibonaccies_until(13) results: [1, 2, 3, 5, 8, 13]
*
*/
std::vector<int> fibonaccies_until(int n){
std::vector<int> result(n + 1);
result[0] = 1;
bool stop = false;
int resultSize = 0;
int v_2 = 1;
int v_1 = 1;
for (int i = 1, limit = n + 1; !stop && i < limit; ++i){
result[i] = v_1 + v_2;
v_2 = v_1;
v_1 = result[i];
if (result[i] > n) {
stop = true;
resultSize = i;
} else if (result[i] == n) {
stop = true;
resultSize = i + 1;
}
}
result.erase(result.begin() + resultSize, result.end());
result.shrink_to_fit();
return result;
}
/**
* Struct to hold the attemp information
*/
struct Attempt {
int position; // current position
int movesCount; // stores the amount of jumps so far
Attempt(int position, int movesCount) {
this->position = position;
this->movesCount = movesCount;
}
};
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <algorithm>
#include <queue>
/**
* Generates the list of fib numbers from 2-th fib until n (inclusive n if n is also fib) in reversed order
*
* For example, the fib number until 10 are: 0, 1, 1, 2, 3, 5, 8
*
* fibonaccies_until(10) results: [1, 2, 3, 5, 8]
*
* and
*
* fibonaccies_until(13) results: [1, 2, 3, 5, 8, 13]
*
*/
std::vector<int> fibonaccies_until(int n){
std::vector<int> result(n + 1);
result[0] = 1;
bool stop = false;
int resultSize = 0;
int v_2 = 1;
int v_1 = 1;
for (int i = 1, limit = n + 1; !stop && i < limit; ++i){
result[i] = v_1 + v_2;
v_2 = v_1;
v_1 = result[i];
if (result[i] > n) {
stop = true;
resultSize = i;
} else if (result[i] == n) {
stop = true;
resultSize = i + 1;
}
}
result.erase(result.begin() + resultSize, result.end());
result.shrink_to_fit();
return result;
}
/**
* Struct to hold the attemp information
*/
struct Attempt {
int position; // current position
int movesCount; // stores the amount of jumps so far
Attempt(int position, int movesCount) {
this->position = position;
this->movesCount = movesCount;
}
};
int solution(vector<int> &A) {
const int N = A.size();
std::vector<int> fibonaccies = fibonaccies_until(N + 1);
std::reverse(fibonaccies.begin(), fibonaccies.end());
// our queue of attempts. We are using Breadth First Search - BFS
// and a queue is a streamline and easy way to implement it
std::queue<Attempt> attempts;
// the first step is set the frog in position -1 and zero moves so far
attempts.push(Attempt(-1, 0));
int result = -1;
// I don't like use break/continue in my loops. A boolean flag is good to avoid it
bool stop = false;
// this is an incredible important check up.
// Check if you visited a position already will help you
// avoid waste time in a wrong path twice
std::vector<bool> visited(N, false);
while (!stop) {
if (attempts.empty()) {
// no more attempts, giving up and returning -1
stop = true;
} else {
Attempt currentAttempt = attempts.front();
if (currentAttempt.position == N) {
// Gotcha! Found!
result = currentAttempt.movesCount;
stop = true;
} else {
attempts.pop();
for (int i = 0, limit = fibonaccies.size(); !stop && i < limit; ++i) {
const int jump = fibonaccies[i];
const int newPosition = currentAttempt.position + jump;
if (newPosition == N) {
// Gotcha! Found!
result = currentAttempt.movesCount + 1;
stop = true;
} else {
// avoiding overflow
bool validPosition = newPosition >= 0;
// checking if the jump is not after N
validPosition = validPosition && newPosition < N;
// checking if there is a leaf
validPosition = validPosition && A[newPosition] != 0;
// checking if this position was not visited before
validPosition = validPosition && !visited[newPosition];
if (validPosition) {
//queueing the new attempt and remember to never came here again
attempts.push(Attempt(newPosition, currentAttempt.movesCount + 1));
visited[newPosition] = true;
}
}
}
}
}
}
return result;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <algorithm>
#include <queue>
/**
* Generates the list of fib numbers from 2-th fib until n (inclusive n if n is also fib) in reversed order
*
* For example, the fib number until 10 are: 0, 1, 1, 2, 3, 5, 8
*
* fibonaccies_until(10) results: [1, 2, 3, 5, 8]
*
* and
*
* fibonaccies_until(13) results: [1, 2, 3, 5, 8, 13]
*
*/
std::vector<int> fibonaccies_until(int n){
std::vector<int> result(n + 1);
result[0] = 1;
bool stop = false;
int resultSize = 0;
int v_2 = 1;
int v_1 = 1;
for (int i = 1, limit = n + 1; !stop && i < limit; ++i){
result[i] = v_1 + v_2;
v_2 = v_1;
v_1 = result[i];
if (result[i] > n) {
stop = true;
resultSize = i;
} else if (result[i] == n) {
stop = true;
resultSize = i + 1;
}
}
result.erase(result.begin() + resultSize, result.end());
result.shrink_to_fit();
return result;
}
/**
* Struct to hold the attemp information
*/
struct Attempt {
int position; // current position
int movesCount; // stores the amount of jumps so far
Attempt(int position, int movesCount) {
this->position = position;
this->movesCount = movesCount;
}
};
int solution(vector<int> &A) {
const int N = A.size();
std::vector<int> fibonaccies = fibonaccies_until(N + 1);
std::reverse(fibonaccies.begin(), fibonaccies.end());
// our queue of attempts. We are using Breadth First Search - BFS
// and a queue is a streamline and easy way to implement it
std::queue<Attempt> attempts;
// the first step is set the frog in position -1 and zero moves so far
attempts.push(Attempt(-1, 0));
int result = -1;
// I don't like use break/continue in my loops. A boolean flag is good to avoid it
bool stop = false;
// this is an incredible important check up.
// Check if you visited a position already will help you
// avoid waste time in a wrong path twice
std::vector<bool> visited(N, false);
while (!stop) {
if (attempts.empty()) {
// no more attempts, giving up and returning -1
stop = true;
} else {
Attempt currentAttempt = attempts.front();
if (currentAttempt.position == N) {
// Gotcha! Found!
result = currentAttempt.movesCount;
stop = true;
} else {
attempts.pop();
for (int i = 0, limit = fibonaccies.size(); !stop && i < limit; ++i) {
const int jump = fibonaccies[i];
const int newPosition = currentAttempt.position + jump;
if (newPosition == N) {
// Gotcha! Found!
result = currentAttempt.movesCount + 1;
stop = true;
} else {
// avoiding overflow
bool validPosition = newPosition >= 0;
// checking if the jump is not after N
validPosition = validPosition && newPosition < N;
// checking if there is a leaf
validPosition = validPosition && A[newPosition] != 0;
// checking if this position was not visited before
validPosition = validPosition && !visited[newPosition];
if (validPosition) {
//queueing the new attempt and remember to never came here again
attempts.push(Attempt(newPosition, currentAttempt.movesCount + 1));
visited[newPosition] = true;
}
}
}
}
}
}
return result;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <algorithm>
#include <queue>
/**
* Generates the list of fib numbers from 2-th fib until n (inclusive n if n is also fib) in reversed order
*
* For example, the fib number until 10 are: 0, 1, 1, 2, 3, 5, 8
*
* fibonaccies_until(10) results: [1, 2, 3, 5, 8]
*
* and
*
* fibonaccies_until(13) results: [1, 2, 3, 5, 8, 13]
*
*/
std::vector<int> fibonaccies_until(int n){
std::vector<int> result(n + 1);
result[0] = 1;
bool stop = false;
int resultSize = 0;
int v_2 = 1;
int v_1 = 1;
for (int i = 1, limit = n + 1; !stop && i < limit; ++i){
result[i] = v_1 + v_2;
v_2 = v_1;
v_1 = result[i];
if (result[i] > n) {
stop = true;
resultSize = i;
} else if (result[i] == n) {
stop = true;
resultSize = i + 1;
}
}
result.erase(result.begin() + resultSize, result.end());
result.shrink_to_fit();
return result;
}
/**
* Struct to hold the attemp information
*/
struct Attempt {
int position; // current position
int movesCount; // stores the amount of jumps so far
Attempt(int position, int movesCount) {
this->position = position;
this->movesCount = movesCount;
}
};
int solution(vector<int> &A) {
const int N = A.size();
std::vector<int> fibonaccies = fibonaccies_until(N + 1);
std::reverse(fibonaccies.begin(), fibonaccies.end());
// our queue of attempts. We are using Breadth First Search - BFS
// and a queue is a streamline and easy way to implement it
std::queue<Attempt> attempts;
// the first step is set the frog in position -1 and zero moves so far
attempts.push(Attempt(-1, 0));
int result = -1;
// I don't like use break/continue in my loops. A boolean flag is good to avoid it
bool stop = false;
// this is an incredible important check up.
// Check if you visited a position already will help you
// avoid waste time in a wrong path twice
std::vector<bool> visited(N, false);
while (!stop) {
if (attempts.empty()) {
// no more attempts, giving up and returning -1
stop = true;
} else {
Attempt currentAttempt = attempts.front();
if (currentAttempt.position == N) {
// Gotcha! Found!
result = currentAttempt.movesCount;
stop = true;
} else {
attempts.pop();
for (int i = 0, limit = fibonaccies.size(); !stop && i < limit; ++i) {
const int jump = fibonaccies[i];
const int newPosition = currentAttempt.position + jump;
if (newPosition == N) {
// Gotcha! Found!
result = currentAttempt.movesCount + 1;
stop = true;
} else {
// avoiding overflow
bool validPosition = newPosition >= 0;
// checking if the jump is not after N
validPosition = validPosition && newPosition < N;
// checking if there is a leaf
validPosition = validPosition && A[newPosition] != 0;
// checking if this position was not visited before
validPosition = validPosition && !visited[newPosition];
if (validPosition) {
//queueing the new attempt and remember to never came here again
attempts.push(Attempt(newPosition, currentAttempt.movesCount + 1));
visited[newPosition] = true;
}
}
}
}
}
}
return result;
}
The solution obtained perfect score.