Date: 2024-07-16
Problem
- The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In mathematical terms, it’s defined as:
- for
- We are tasked with finding the Nth Fibonacci number given an input integer N.
Example:
Approach 1:
- The recursive approach is not optimal due to the time complexity of . This happens because each recursive call makes two further recursive calls, resulting in overlapping subproblems.
Implementation
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 2:
- To avoid redundant calculations, memoization can be used to store previously computed values of Fibonacci numbers, significantly improving efficiency.
Implementation
Complexity Analysis
- Time Complexity:
- Space Complexity:
Approach 3:
- The iterative solution is the most optimal. It uses an array initialized with the base Fibonacci values
[0, 1]
and updates the array by stepping through the Fibonacci sequence, calculating each value iteratively.
Implementation
Complexity Analysis
- Time Complexity:
- Space Complexity: