Skip to content

An improvement on Ficbonacci function using DP #81

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

Closed
duongtq opened this issue Jan 3, 2019 · 15 comments
Closed

An improvement on Ficbonacci function using DP #81

duongtq opened this issue Jan 3, 2019 · 15 comments
Labels
on hold Being discussed by the maintainers

Comments

@duongtq
Copy link

duongtq commented Jan 3, 2019

// My code just improves a little
// Feel free to ask me questions

function dp_ficbo(n)
{
var val = [];
for ( let i = 0; i <= n; i++)
{
val[i] = 0;
}

	if ( n == 1 || n == 2)
	{
		return 1;
	}
	else
	{
		val[1] = 1;
		val[2] = 2;

		for ( let i = 3; i <= n; i++ )
		{
			val[i] = val[i - 1] + val[i - 2];
		}
	}

	return val[n - 1];
}

console.log(dp_ficbo(20));

@ms10398
Copy link
Member

ms10398 commented Jan 3, 2019

If you feel you can make a better implementation feel free to make a PR

@duongtq
Copy link
Author

duongtq commented Jan 3, 2019

I do not understand what PR means. If it is a sarcasm feel free not to reply.

@ms10398
Copy link
Member

ms10398 commented Jan 3, 2019

No it is not sarcasm

Sorry if you misunderstood

PR means Pull Request which means updating the code of the repo with the better approach so that I can merge it in the repository.

I have given to link for you to check just in case if you are new to GitHub

https://help.github.com/articles/about-pull-requests/

@duongtq
Copy link
Author

duongtq commented Jan 3, 2019

Thank you for your explanation.
I will use pull request to share the code.

@christianbender
Copy link

@duongtq This lines are needless

for ( let i = 0; i <= n; i++)
{
    val[i] = 0;
}

Because you used dynamic arrays in javascript.

@christianbender christianbender added the on hold Being discussed by the maintainers label Feb 17, 2019
@christianbender
Copy link

@duongtq

val[1] = 1;
val[2] = 2;

Begin with index 0. Change your for-loop according to index 0

@archanaserver
Copy link

I would like to work on this by providing the solution of the Fibonacci sequence from the Brute Force Approach to the Top-Down and Bottom-Up Dynamic Approach.

@ritwikchakraborty123
Copy link

can i make a pull request...i can update the code by applying dynamic programming.

@Satzyakiz
Copy link
Contributor

Is this issue still open ? I would like to work on it.

@itsvinayak
Copy link
Member

itsvinayak commented May 8, 2020

there is already this function available

const dict = new Map()

const FibonacciRecursiveDP = (stairs) => {
  if (stairs <= 0) return 0
  if (stairs === 1) return 1

  // Memoize stair count
  if (dict.has(stairs)) return dict.get(stairs)

  const res =
    FibonacciRecursiveDP(stairs - 1) + FibonacciRecursiveDP(stairs - 2)

  dict.set(stairs, res)

  return res
}


if you can improve it or add something new then go for it @Satzyakiz, you are most welcome

@Satzyakiz
Copy link
Contributor

Satzyakiz commented May 8, 2020

Maybe something like this :

function fibonacci(n) {
  const fibArr = [0,1];

  for(let i=2; i<=n; i++){
    fibArr.push( fibArr[i-1] + fibArr[i-2] );
  }

  return fibArr[n-1];
}

This is nothing new, just avoiding the recursive calls. @itsvinayak

@itsvinayak
Copy link
Member

itsvinayak commented May 8, 2020

@Satzyakiz you can make a separate function for this approach in this file and send a PR

@Satzyakiz
Copy link
Contributor

Where should I keep it ? In Algorithms/Dynamic-Programming/ ?

@itsvinayak
Copy link
Member

itsvinayak commented May 8, 2020 via email

@itsvinayak
Copy link
Member

many new algorithms are added on this topic #159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold Being discussed by the maintainers
Projects
None yet
Development

No branches or pull requests

7 participants