Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded dynamic fibonacci algorithm #523
Open
+52
−0
Conversation
added 2 commits
Sep 30, 2018
| */ | ||
| public class Fibonacci { | ||
|
|
||
| private static HashMap<Integer, Integer> fibonacciCache = new HashMap<>(); |
yejjuritesh
May 3, 2020
Instead of a HashMap, we can do this with constant space by using two variables.
Instead of a HashMap, we can do this with constant space by using two variables.
yejjuritesh
May 3, 2020
Also, An Array or ArrayList can be used instead of HashMap. Because the index of array can be assumed as the position of fibbonaci number :
index of Array- fibbonaci number
0 - 1
1 - 1
2 - 2
3 - 3
4 - 5
5 - 8
So, nth fibonacci number will be present at nth index of array.
Also, An Array or ArrayList can be used instead of HashMap. Because the index of array can be assumed as the position of fibbonaci number :
index of Array- fibbonaci number
0 - 1
1 - 1
2 - 2
3 - 3
4 - 5
5 - 8
So, nth fibonacci number will be present at nth index of array.
| * | ||
| * @return the nth fibonacci number. | ||
| */ | ||
| public int fibonacci(int n) { |
yejjuritesh
May 3, 2020
Instead of a recursive approach or top-down dynamic approach, A bottom-up approach will do the trick.
Instead of a recursive approach or top-down dynamic approach, A bottom-up approach will do the trick.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Due to the conversation with @varunu28 I now made a fibonacci algorithm that (hopefully) is dynamic programming