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 <= 1) return n;
return fibo(n - 1) + fibo(n - 2);
}
/**
* 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(n)
*/
public static int fiboTopDown(int n) {
if (n < 0) throw new IllegalArgumentException("The value of n: " + n + " should be positive.");
final Map<Integer, Integer> fiboCache = new HashMap<Integer, Integer>();
return fiboCompute(n, fiboCache);
}
private static int fiboCompute(int n, Map<Integer, Integer> 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) {
if (n < 0) throw new IllegalArgumentException("The value of n: " + n + " should be positive.");
int a = 0;
int b = 1;
int c = 0;
for (int i = 0; i < n; i++) {
a = b;
b = c;
c = a + b;
}
return c;
}
/**
* 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.
*
* @param numStairs
* @return
*/
public static int stairCount(int numStairs) {
if (numStairs <= 0) throw new IllegalArgumentException("The number of stairs: " + numStairs + "
should be positive.");
return fiboBottomUp(numStairs + 1);
}
public static void main(String[] args) {
System.out.println(fibo(0) + ":" + fibo(1) + ":" + fibo(2) + ":" + fibo(3) + ":" + fibo(4) + ":" + fibo(5));
System.out.println(fiboTopDown(0) + ":" + fiboTopDown(1) + ":" + fiboTopDown(2) + ":"
+ fiboTopDown(3) + ":" + fiboTopDown(4) + ":" + fiboTopDown(5));
System.out.println(fiboBottomUp(0) + ":" + fiboBottomUp(1) + ":"
+ fiboBottomUp(2) + ":" + fiboBottomUp(3) + ":"
+ fiboBottomUp(4) + ":" + fiboBottomUp(5));
System.out.println(stairCount(1) + ":"
+ stairCount(2) + ":" + stairCount(3) + ":"
+ stairCount(4) + ":" + stairCount(5));
}
}