patternjavaMinor
Fibonacci series, topdown and bottom up approaches, stairway climbing
Viewed 0 times
fibonaccibottomtopdownstairwayandseriesapproachesclimbing
Problem
I have solved fibonacci series and then used the topdown and bottom up approaches to solve it. Also I have included the stairway climbing question as follows "You are climbing a stair case. Each time you can either make 1 step or 2 steps. The staircase has numStairs steps. Returns In how many distinct ways can you climb the staircase."
Looking for code review, optimizations, best practices etc.
```
public final class Fibo {
private Fibo() {}
/**
* Returns the nth number in the fibonacci sequence.
*
* @param n nth position in the fibo series, which starts from 0th position.
* @return the nth number in the fibonacci series.
*/
/*
* TimeComplexity: O(n)
* Space Complexity: http://www.geeksforgeeks.org/g-fact-86/
*/
public static int fibo(int n) {
if (n fiboCache = new HashMap();
return fiboCompute(n, fiboCache);
}
private static int fiboCompute(int n, Map fiboCache) {
if (n <= 1) return n;
if (fiboCache.containsKey(n)) {
return fiboCache.get(n);
}
int sum = fiboCompute (n - 1, fiboCache) + fiboCompute (n - 2, fiboCache);
fiboCache.put(n, sum);
return sum;
}
/**
* Returns the nth number in the fibonacci sequence.
*
* @param n nth position in the fibo series, which starts from 0th position.
* @return the nth number in the fibonacci series.
*/
/*
* Time complexity: O(n)
* Aux Space: O(1)
*/
public static int fiboBottomUp(int n) {
Looking for code review, optimizations, best practices etc.
```
public final class Fibo {
private Fibo() {}
/**
* Returns the nth number in the fibonacci sequence.
*
* @param n nth position in the fibo series, which starts from 0th position.
* @return the nth number in the fibonacci series.
*/
/*
* TimeComplexity: O(n)
* Space Complexity: http://www.geeksforgeeks.org/g-fact-86/
*/
public static int fibo(int n) {
if (n fiboCache = new HashMap();
return fiboCompute(n, fiboCache);
}
private static int fiboCompute(int n, Map fiboCache) {
if (n <= 1) return n;
if (fiboCache.containsKey(n)) {
return fiboCache.get(n);
}
int sum = fiboCompute (n - 1, fiboCache) + fiboCompute (n - 2, fiboCache);
fiboCache.put(n, sum);
return sum;
}
/**
* Returns the nth number in the fibonacci sequence.
*
* @param n nth position in the fibo series, which starts from 0th position.
* @return the nth number in the fibonacci series.
*/
/*
* Time complexity: O(n)
* Aux Space: O(1)
*/
public static int fiboBottomUp(int n) {
Solution
This comment is inaccurate:
A naïve recursive
In
Using a
Consider widening your return types to
/*
* TimeComplexity: O(n)
* Space Complexity: http://www.geeksforgeeks.org/g-fact-86/
*/
public static int fibo(int n) { … }A naïve recursive
fibo() has O(2n) time complexity. Think of it this way: to calculate fibo(n), you break it up into two problems, each of size n - 1.In
fiboBottomUp(), don't declare/define int a = 0, since it is only ever used as a temporary variable inside the for-loop.public static int fiboBottomUp(int n) {
if (n < 0) {
// Message is inaccurate: n = 0 is allowable
throw new IllegalArgumentException("The value of n: " + n + " should be non-negative.");
}
int b = 1;
int c = 0;
for (int i = 0; i < n; i++) {
int a = b;
b = c;
c = a + b;
}
return c;
}Using a
HashMap for fiboCache is more complicated than necessary. An ArrayList or even an int[n + 1] will do, since all the keys are consecutive integers.Consider widening your return types to
long to stave off overflow for a while longer.Code Snippets
/*
* TimeComplexity: O(n)
* Space Complexity: http://www.geeksforgeeks.org/g-fact-86/
*/
public static int fibo(int n) { … }public static int fiboBottomUp(int n) {
if (n < 0) {
// Message is inaccurate: n = 0 is allowable
throw new IllegalArgumentException("The value of n: " + n + " should be non-negative.");
}
int b = 1;
int c = 0;
for (int i = 0; i < n; i++) {
int a = b;
b = c;
c = a + b;
}
return c;
}Context
StackExchange Code Review Q#41441, answer score: 5
Revisions (0)
No revisions yet.