mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
@ -1,9 +1,8 @@
|
||||
package DynamicProgramming;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
@ -13,14 +12,15 @@ public class Fibonacci {
|
||||
|
||||
private static Map<Integer, Integer> map = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
int n = Integer.parseInt(br.readLine());
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
|
||||
System.out.println(fibMemo(n));
|
||||
System.out.println(fibBotUp(n));
|
||||
System.out.println(fibOptimized(n));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -29,7 +29,7 @@ public class Fibonacci {
|
||||
* @param n The input n for which we have to determine the fibonacci number
|
||||
* Outputs the nth fibonacci number
|
||||
**/
|
||||
private static int fibMemo(int n) {
|
||||
public static int fibMemo(int n) {
|
||||
if (map.containsKey(n)) {
|
||||
return map.get(n);
|
||||
}
|
||||
@ -51,7 +51,7 @@ public class Fibonacci {
|
||||
* @param n The input n for which we have to determine the fibonacci number
|
||||
* Outputs the nth fibonacci number
|
||||
**/
|
||||
private static int fibBotUp(int n) {
|
||||
public static int fibBotUp(int n) {
|
||||
|
||||
Map<Integer, Integer> fib = new HashMap<>();
|
||||
|
||||
@ -83,7 +83,7 @@ public class Fibonacci {
|
||||
* Whereas , the above functions will take O(n) Space.
|
||||
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
|
||||
**/
|
||||
private static int fibOptimized(int n) {
|
||||
public static int fibOptimized(int n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user