The Wayback Machine - https://web.archive.org/web/20201103190310/https://github.com/TheAlgorithms/Java/pull/523
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic fibonacci algorithm #523

Open
wants to merge 2 commits into
base: Development
from

Conversation

@CraxyTM
Copy link

@CraxyTM CraxyTM commented Oct 1, 2018

Due to the conversation with @varunu28 I now made a fibonacci algorithm that (hopefully) is dynamic programming

*/
public class Fibonacci {

private static HashMap<Integer, Integer> fibonacciCache = new HashMap<>();

This comment has been minimized.

@yejjuritesh

yejjuritesh May 3, 2020

Instead of a HashMap, we can do this with constant space by using two variables.

This comment has been minimized.

@yejjuritesh

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.

*
* @return the nth fibonacci number.
*/
public int fibonacci(int n) {

This comment has been minimized.

@yejjuritesh

yejjuritesh May 3, 2020

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
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.