2019-05-09 19:32:54 +08:00
|
|
|
package DynamicProgramming;
|
|
|
|
|
2017-09-05 05:08:12 +08:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-10-24 14:38:08 +08:00
|
|
|
import java.util.Scanner;
|
2017-09-05 05:08:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class Fibonacci {
|
|
|
|
|
2019-02-05 13:10:40 +08:00
|
|
|
private static Map<Integer, Integer> map = new HashMap<>();
|
2017-09-05 05:08:12 +08:00
|
|
|
|
|
|
|
|
2019-10-24 14:38:08 +08:00
|
|
|
public static void main(String[] args) {
|
2017-09-05 05:08:12 +08:00
|
|
|
|
2019-02-27 06:10:20 +08:00
|
|
|
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
2019-10-24 14:38:08 +08:00
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
int n = sc.nextInt();
|
|
|
|
|
2019-02-27 06:10:20 +08:00
|
|
|
System.out.println(fibMemo(n));
|
|
|
|
System.out.println(fibBotUp(n));
|
2019-10-24 14:38:08 +08:00
|
|
|
System.out.println(fibOptimized(n));
|
2017-09-05 05:08:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method finds the nth fibonacci number using memoization technique
|
|
|
|
*
|
|
|
|
* @param n The input n for which we have to determine the fibonacci number
|
2019-02-05 13:10:40 +08:00
|
|
|
* Outputs the nth fibonacci number
|
2017-09-05 05:08:12 +08:00
|
|
|
**/
|
2019-10-24 14:38:08 +08:00
|
|
|
public static int fibMemo(int n) {
|
2017-09-05 05:08:12 +08:00
|
|
|
if (map.containsKey(n)) {
|
|
|
|
return map.get(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int f;
|
|
|
|
|
2019-02-27 06:10:20 +08:00
|
|
|
if (n <= 1) {
|
|
|
|
f = n;
|
2019-02-05 13:10:40 +08:00
|
|
|
} else {
|
|
|
|
f = fibMemo(n - 1) + fibMemo(n - 2);
|
|
|
|
map.put(n, f);
|
2017-09-05 05:08:12 +08:00
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
2017-09-05 05:40:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method finds the nth fibonacci number using bottom up
|
|
|
|
*
|
|
|
|
* @param n The input n for which we have to determine the fibonacci number
|
2019-02-05 13:10:40 +08:00
|
|
|
* Outputs the nth fibonacci number
|
2017-09-05 05:40:12 +08:00
|
|
|
**/
|
2019-10-24 14:38:08 +08:00
|
|
|
public static int fibBotUp(int n) {
|
2017-09-05 05:40:12 +08:00
|
|
|
|
2019-02-05 13:10:40 +08:00
|
|
|
Map<Integer, Integer> fib = new HashMap<>();
|
2017-09-05 05:40:12 +08:00
|
|
|
|
2019-02-27 06:10:20 +08:00
|
|
|
for (int i = 0; i <= n; i++) {
|
2019-02-05 13:10:40 +08:00
|
|
|
int f;
|
2019-02-27 06:10:20 +08:00
|
|
|
if (i <= 1) {
|
|
|
|
f = i;
|
2019-02-05 13:10:40 +08:00
|
|
|
} else {
|
|
|
|
f = fib.get(i - 1) + fib.get(i - 2);
|
2017-09-05 05:40:12 +08:00
|
|
|
}
|
|
|
|
fib.put(i, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fib.get(n);
|
|
|
|
}
|
2019-02-05 13:10:40 +08:00
|
|
|
|
|
|
|
|
2018-11-21 07:52:24 +08:00
|
|
|
/**
|
|
|
|
* This method finds the nth fibonacci number using bottom up
|
|
|
|
*
|
|
|
|
* @param n The input n for which we have to determine the fibonacci number
|
2019-02-05 13:10:40 +08:00
|
|
|
* Outputs the nth fibonacci number
|
|
|
|
* <p>
|
|
|
|
* This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
|
|
|
|
* It saves both memory and time.
|
|
|
|
* Space Complexity will be O(1)
|
|
|
|
* Time Complexity will be O(n)
|
|
|
|
* <p>
|
|
|
|
* Whereas , the above functions will take O(n) Space.
|
|
|
|
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
|
2018-11-21 07:52:24 +08:00
|
|
|
**/
|
2019-10-24 14:38:08 +08:00
|
|
|
public static int fibOptimized(int n) {
|
2018-11-21 07:52:24 +08:00
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-05 13:10:40 +08:00
|
|
|
int prev = 0, res = 1, next;
|
2019-10-11 00:20:35 +08:00
|
|
|
for (int i = 2; i <= n; i++) {
|
2019-02-05 13:10:40 +08:00
|
|
|
next = prev + res;
|
|
|
|
prev = res;
|
|
|
|
res = next;
|
2018-11-21 07:52:24 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-10-24 14:38:08 +08:00
|
|
|
}
|