11package DynamicProgramming ;
22
3- import java .io .BufferedReader ;
4- import java .io .InputStreamReader ;
53import java .util .HashMap ;
64import java .util .Map ;
5+ import java .util .Scanner ;
76
87/**
98 * @author Varun Upadhyay (https://github.com/varunu28)
@@ -13,14 +12,15 @@ public class Fibonacci {
1312
1413 private static Map <Integer , Integer > map = new HashMap <>();
1514
16- public static void main (String [] args ) throws Exception {
17-
18- BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
19- int n = Integer .parseInt (br .readLine ());
15+ public static void main (String [] args ) {
2016
2117 // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
18+ Scanner sc = new Scanner (System .in );
19+ int n = sc .nextInt ();
20+
2221 System .out .println (fibMemo (n ));
2322 System .out .println (fibBotUp (n ));
23+ System .out .println (fibOptimized (n ));
2424 }
2525
2626 /**
@@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
2929 * @param n The input n for which we have to determine the fibonacci number
3030 * Outputs the nth fibonacci number
3131 **/
32- private static int fibMemo (int n ) {
32+ public static int fibMemo (int n ) {
3333 if (map .containsKey (n )) {
3434 return map .get (n );
3535 }
@@ -51,7 +51,7 @@ private static int fibMemo(int n) {
5151 * @param n The input n for which we have to determine the fibonacci number
5252 * Outputs the nth fibonacci number
5353 **/
54- private static int fibBotUp (int n ) {
54+ public static int fibBotUp (int n ) {
5555
5656 Map <Integer , Integer > fib = new HashMap <>();
5757
@@ -83,7 +83,7 @@ private static int fibBotUp(int n) {
8383 * Whereas , the above functions will take O(n) Space.
8484 * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
8585 **/
86- private static int fibOptimized (int n ) {
86+ public static int fibOptimized (int n ) {
8787 if (n == 0 ) {
8888 return 0 ;
8989 }
@@ -95,4 +95,4 @@ private static int fibOptimized(int n) {
9595 }
9696 return res ;
9797 }
98- }
98+ }
0 commit comments