1
1
package DynamicProgramming ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .InputStreamReader ;
5
3
import java .util .HashMap ;
6
4
import java .util .Map ;
5
+ import java .util .Scanner ;
7
6
8
7
/**
9
8
* @author Varun Upadhyay (https://github.com/varunu28)
@@ -13,14 +12,15 @@ public class Fibonacci {
13
12
14
13
private static Map <Integer , Integer > map = new HashMap <>();
15
14
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 ) {
20
16
21
17
// 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
+
22
21
System .out .println (fibMemo (n ));
23
22
System .out .println (fibBotUp (n ));
23
+ System .out .println (fibOptimized (n ));
24
24
}
25
25
26
26
/**
@@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
29
29
* @param n The input n for which we have to determine the fibonacci number
30
30
* Outputs the nth fibonacci number
31
31
**/
32
- private static int fibMemo (int n ) {
32
+ public static int fibMemo (int n ) {
33
33
if (map .containsKey (n )) {
34
34
return map .get (n );
35
35
}
@@ -51,7 +51,7 @@ private static int fibMemo(int n) {
51
51
* @param n The input n for which we have to determine the fibonacci number
52
52
* Outputs the nth fibonacci number
53
53
**/
54
- private static int fibBotUp (int n ) {
54
+ public static int fibBotUp (int n ) {
55
55
56
56
Map <Integer , Integer > fib = new HashMap <>();
57
57
@@ -83,7 +83,7 @@ private static int fibBotUp(int n) {
83
83
* Whereas , the above functions will take O(n) Space.
84
84
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
85
85
**/
86
- private static int fibOptimized (int n ) {
86
+ public static int fibOptimized (int n ) {
87
87
if (n == 0 ) {
88
88
return 0 ;
89
89
}
@@ -95,4 +95,4 @@ private static int fibOptimized(int n) {
95
95
}
96
96
return res ;
97
97
}
98
- }
98
+ }
0 commit comments