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.
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:
- Generate all Fibonacci numbers up to the river length + 1.
- Use BFS to explore all reachable positions, starting from -1 (before the river).
- For each position, try all possible Fibonacci jumps.
- If a jump lands on a leaf or the other side, enqueue the new position and increment the jump count.
- Track visited positions to avoid cycles.
- 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), wherenis the river length andkis the number of Fibonacci numbers. - Space complexity:
O(n).