title: Count the minimum number of jumps required for a frog to get to the other side of a river
body: <p>I work with a Codility problem provided below, </p>
  
  <p>The Fibonacci sequence is defined using the following recursive formula:</p>
  
  <pre><code>F(0) = 0
  F(1) = 1
  F(M) = F(M - 1) + F(M - 2) if M &gt;= 2
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <p>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.</p>
  
  <p>For example, consider array A such that:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The frog can make three jumps of length F(5) = 5, F(3) = 2 and F(5) = 5.</p>
  
  <p>Write a function:</p>
  
  <pre><code>class Solution { public int solution(int[] A); }
  </code></pre>
  
  <p>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.</p>
  
  <p>For example, given:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>the function should return 3, as explained above.</p>
  
  <p>Assume that:</p>
  
  <p>N is an integer within the range <code>[0..100,000]</code>;
  each element of array A is an integer that can have one of the following values: 0, 1.
  Complexity:</p>
  
  <p>expected worst-case time complexity is <code>O(N*log(N))</code>;
  expected worst-case space complexity is <code>O(N)</code> (not counting the storage required for input arguments).</p>
  
  <p>I wrote the following solution, </p>
  
  <pre><code>class Solution {
      private class Jump {
          int position;
          int number;
  
          public int getPosition() {
              return position;
          }
  
          public int getNumber() {
              return number;
          }
  
          public Jump(int pos, int number) {
              this.position = pos;
              this.number = number;
          }
      }
  
      public int solution(int[] A) {
  
          int N = A.length;
  
          List&lt;Integer&gt; fibs = getFibonacciNumbers(N + 1);
  
          Stack&lt;Jump&gt; jumps = new Stack&lt;&gt;();
          jumps.push(new Jump(-1, 0));
  
          boolean[] visited = new boolean[N];
  
          while (!jumps.isEmpty()) {
  
              Jump jump = jumps.pop();
  
              int position = jump.getPosition();
              int number = jump.getNumber();
  
              for (int fib : fibs) {
  
                  if (position + fib &gt; N) {
                      break;
                  } else if (position + fib == N) {
                      return number + 1;
                  } else if (!visited[position + fib] &amp;&amp; A[position + fib] == 1) {
  
                      visited[position + fib] = true;
                      jumps.add(new Jump(position + fib, number + 1));
                  }
              }
          }
  
          return -1;
      }
  
  
      private List&lt;Integer&gt; getFibonacciNumbers(int N) {
  
          List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; 2; i++) {
              list.add(i);
          }
  
          int i = 2;
  
          while (list.get(list.size() - 1) &lt;= N) {
  
              list.add(i, (list.get(i - 1) + list.get(i - 2)));
              i++;
          }
  
          for (i = 0; i &lt; 2; i++) {
              list.remove(i);
          }
  
          return list;
      }
  
  
  
  
      public static void main(String[] args) {
  
      int[] A = new int[11];
  
      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;
  
      System.out.println(solution(A));
     }
  }
  </code></pre>
  
  <p>However, while the correctness seems good, the performance is not high enough.  Is there a bug in the code and how do I improve the performance?</p>
  
  <p><a href="https://i.stack.imgur.com/nknZO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nknZO.png" alt="enter image description here"></a></p>