Skip to content

Commit 2da2ca9

Browse files
Create 0509 - Fibonacci-Numbers.md
Add solution to the leetcode problem 0509 - Fibonacci Numbers
1 parent 57cd239 commit 2da2ca9

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
id: Fibonacci-Numbers
3+
title: Fibonacci-Numbers
4+
sidebar_label: Fibonacci-Numbers
5+
tags:
6+
- Fibonacci
7+
- Dynamic Programming
8+
- Recursion
9+
- Algorithms
10+
---
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ |
16+
| [Fibonacci-Numbers](https://leetcode.com/problems/Fibonacci-Numbers/description/) | [Fibonacci-Numbers Solution on LeetCode](https://leetcode.com/problems/Fibonacci-Numbers/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) |
17+
18+
## Problem Description
19+
20+
The Fibonacci numbers, commonly denoted F(n), form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
21+
22+
- F(0) = 0
23+
- F(1) = 1
24+
- F(n) = F(n-1) + F(n-2) for n > 1
25+
26+
Given an integer n, calculate F(n).
27+
28+
### Examples
29+
30+
### Example 1
31+
**Input:** `n = 2`
32+
**Output:** `1`
33+
**Explanation:** `F(2) = F(1) + F(0) = 1 + 0 = 1.`
34+
35+
### Example 2
36+
**Input:** `n = 3`
37+
**Output:** `2`
38+
**Explanation:** `F(3) = F(2) + F(1) = 1 + 1 = 2.`
39+
40+
### Example 3
41+
**Input:** `n = 4`
42+
**Output:** `3`
43+
**Explanation:** `F(4) = F(3) + F(2) = 2 + 1 = 3.`
44+
45+
## Constraints
46+
47+
- `0 <= n <= 30`
48+
49+
## Approach
50+
51+
There are several approaches to solve the Fibonacci problem:
52+
1. **Recursive Approach**: Simple but inefficient due to redundant calculations.
53+
2. **Iterative Approach**: Efficient using a loop to build up the Fibonacci sequence.
54+
3. **Dynamic Programming Approach**: Efficiently store computed results to avoid redundant calculations.
55+
56+
## Solution in different languages
57+
58+
### Solution in Python
59+
```python
60+
def fibonacci(n):
61+
if n == 0:
62+
return 0
63+
elif n == 1:
64+
return 1
65+
66+
prev1, prev2 = 0, 1
67+
for i in range(2, n + 1):
68+
current = prev1 + prev2
69+
prev1, prev2 = prev2, current
70+
71+
return prev2
72+
73+
# Example usage:
74+
n = 4
75+
print(fibonacci(n)) # Output: 3
76+
```
77+
78+
### Solution in Java
79+
80+
```java
81+
public class Solution {
82+
public int fibonacci(int n) {
83+
if (n == 0) {
84+
return 0;
85+
} else if (n == 1) {
86+
return 1;
87+
}
88+
89+
int prev1 = 0, prev2 = 1;
90+
for (int i = 2; i <= n; i++) {
91+
int current = prev1 + prev2;
92+
prev1 = prev2;
93+
prev2 = current;
94+
}
95+
96+
return prev2;
97+
}
98+
99+
public static void main(String[] args) {
100+
Solution solution = new Solution();
101+
int n = 4;
102+
System.out.println(solution.fibonacci(n)); // Output: 3
103+
}
104+
}
105+
```
106+
107+
### Solution in C++
108+
109+
```cpp
110+
#include <iostream>
111+
using namespace std;
112+
113+
int fibonacci(int n) {
114+
if (n == 0) {
115+
return 0;
116+
} else if (n == 1) {
117+
return 1;
118+
}
119+
120+
int prev1 = 0, prev2 = 1;
121+
for (int i = 2; i <= n; i++) {
122+
int current = prev1 + prev2;
123+
prev1 = prev2;
124+
prev2 = current;
125+
}
126+
127+
return prev2;
128+
}
129+
130+
int main() {
131+
int n = 4;
132+
cout << fibonacci(n) << endl; // Output: 3
133+
return 0;
134+
}
135+
```
136+
137+
### Solution in C
138+
139+
```c
140+
#include <stdio.h>
141+
142+
int fibonacci(int n) {
143+
if (n == 0) {
144+
return 0;
145+
} else if (n == 1) {
146+
return 1;
147+
}
148+
149+
int prev1 = 0, prev2 = 1;
150+
for (int i = 2; i <= n; i++) {
151+
int current = prev1 + prev2;
152+
prev1 = prev2;
153+
prev2 = current;
154+
}
155+
156+
return prev2;
157+
}
158+
159+
int main() {
160+
int n = 4;
161+
printf("%d\n", fibonacci(n)); // Output: 3
162+
return 0;
163+
}
164+
```
165+
166+
### Solution in JavaScript
167+
168+
```javascript
169+
function fibonacci(n) {
170+
if (n === 0) {
171+
return 0;
172+
} else if (n === 1) {
173+
return 1;
174+
}
175+
176+
let prev1 = 0, prev2 = 1;
177+
for (let i = 2; i <= n; i++) {
178+
let current = prev1 + prev2;
179+
prev1 = prev2;
180+
prev2 = current;
181+
}
182+
183+
return prev2;
184+
}
185+
186+
// Example usage:
187+
let n = 4;
188+
console.log(fibonacci(n)); // Output: 3
189+
```
190+
191+
## Step-by-Step Algorithm
192+
193+
1. **Base Cases**:
194+
- If \( n = 0 \), return 0.
195+
- If \( n = 1 \), return 1.
196+
197+
2. **Iterative Calculation**:
198+
- Initialize `prev1` to 0 and `prev2` to 1.
199+
- Iterate from 2 to n:
200+
- Calculate `current` as `prev1 + prev2`.
201+
- Update `prev1` to `prev2` and `prev2` to `current`.
202+
- After the loop, `prev2` contains the Fibonacci number for \( n \).
203+
204+
## Conclusion
205+
206+
The Fibonacci sequence can be efficiently computed using iterative or dynamic programming approaches to avoid redundant calculations. These methods provide a clear improvement over the naive recursive approach, especially for larger values of \( n \). The iterative approach is straightforward and runs in \( O(n) \) time complexity with \( O(1) \) space complexity, making it suitable for the given constraints \( 0 \leq n \leq 30 \).

0 commit comments

Comments
 (0)