Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 1.04 KB

File metadata and controls

22 lines (17 loc) · 1.04 KB

Description: Given a river represented by an array, where 1 is a leaf and 0 is water, a frog starts before the first position and wants to reach the other side. The frog can jump any Fibonacci number of steps. Find the minimal number of jumps needed to cross the river, or -1 if impossible.

Examples

Example 1:

Input: river = [0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0] Output: 3

Algorithmic Steps This problem is solved using BFS and Fibonacci numbers:

  1. Generate all Fibonacci numbers up to the river length + 1.
  2. Use BFS to explore all reachable positions, starting from -1 (before the river).
  3. For each position, try all possible Fibonacci jumps.
  4. If a jump lands on a leaf or the other side, enqueue the new position and increment the jump count.
  5. Track visited positions to avoid cycles.
  6. Return the minimal number of jumps to reach the other side, or -1 if not possible.

Time and Space complexity:

  • Time complexity: O(n * k), where n is the river length and k is the number of Fibonacci numbers.
  • Space complexity: O(n).