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:
function 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.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N + 1) break;
fibs.push(nextFib);
index++;
}
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N + 1) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
A.
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N + 1) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N + 1) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N + 1) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (index + fibs[fibIndex] === N) break;
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
[ 1, 1, 2, 3, 5, 8 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
[ 1, 1, 2, 3, 5, 8 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index <= N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
[ 1, 1, 2, 3, 5, 8 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
[ 1, 1, 2, 3, 5, 8 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fi
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(-1);
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = A.map((value) =>)
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = A.map((value, index) => index)
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable =
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] || )
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
console.log(i);
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ] 6 11
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
for (let j = 0; j < fibs)
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
for (let j = 0; j < fibs.length; j++) {
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev =
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i -
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] )
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
}
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Nu)
}
}
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
/*index = -1;
let count = 0;
while (index < N) {
let fibIndex = fibs.length - 1;
while (A[index + fibs[fibIndex]] !== 1) {
if (fibIndex === 0) return -1;
fibIndex--;
}
count++;
index += fibs[fibIndex];
}
return count;*/
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 1, 1, 0, 2, 0, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N]) {
return reachable[N];
} else {
return -1;
}
}
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N]) {
return reachable[N];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N];
} else {
return -1;
}
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 1, 1, 0, 2, 0, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 1, 1, 0, 2, 0, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[i]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[ 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 ] [ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 1, 1, 0, 2, 0, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fib]) {
reachable[i] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i]]) {
reachable[fibs[i]] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[ 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 ] [ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 1, 2, 0, 2, 0, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] ]) {
reachable[fibs[i]] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[ 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 ] [ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 3 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
console.log(A);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[1, 1, 0, 0, 0]
[ 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 ] [ 1, 1, 2, 3, 5, 8 ] [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 3 ]
function result: 2
[ 1, 1, 0, 0, 0, 1 ] [ 1, 1, 2, 3, 5 ] [ 1, 1, 0, 0, 0, 0 ] [ 1, 1, 0, 0, 0, 2 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
console.log(fibs);
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
console.log(reachable);
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
console.log(reachable);
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
// get the minimal jump count to
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
// get the minimal jump count to reach this leaf
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
// get the minimal jump count to reach this leaf
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[1, 1, 0, 0, 0]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
// get the minimal jump count to reach this leaf
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
[1, 1, 0, 0, 0]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// it is valid to step on the other shore
A.push(1);
// calculate Fibonacci numbers until N
const N = A.length;
const fibs = [1, 1];
let index = 2;
while (true) {
const nextFib = fibs[index - 1] + fibs[index - 2];
if (nextFib > N) break;
fibs.push(nextFib);
index++;
}
// reachable leaves from starting point
const reachable = new Array(N).fill(0);
for (let i = 0; i < fibs.length; i++) {
if (A[fibs[i] - 1]) {
reachable[fibs[i] - 1] = 1;
}
}
for (let i = 0; i < N; i++) {
if (A[i] && !reachable[i]) {
// get the minimal jump count to reach this leaf
let minJumps = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < fibs.length; j++) {
const prev = i - fibs[j];
if (reachable[prev] && minJumps > reachable[prev]) {
minJumps = reachable[prev];
}
}
if (minJumps !== Number.MAX_SAFE_INTEGER) {
reachable[i] = minJumps + 1;
}
}
}
if (reachable[N - 1]) {
return reachable[N - 1];
} else {
return -1;
}
}
The solution obtained perfect score.