-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciDynamicProgrammingExample.java
More file actions
67 lines (54 loc) · 1.32 KB
/
FibonacciDynamicProgrammingExample.java
File metadata and controls
67 lines (54 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Hashtable;
public class FibonacciDynamicProgrammingExample
{
/**
* Memoization -- caches results of pre-calculated subproblems
*/
public static Hashtable<Integer, Integer> memo = new Hashtable<Integer, Integer>();
/**
* computes nth fibonacci number using "careful recursion" aka memoization
*/
public static int fibonacci(int n)
{
// if the fibonacci number was calculated before, return it
if(memo.containsKey(n))
return memo.get(n);
// the nth fib number to return
int f;
if(n <= 2)
f = 1;
else
f = fibonacci(n-1) + fibonacci(n-2);
memo.put(n, f);
return f;
}
/**
* Although the fibonacci() function and this function
* have the same complexity, this is considered more efficient
* because a recursive function will use progressively more memory on
* the stack
*/
public static int bottomUpFib(int n)
{
// the nth fib number to return
int f = -999;
for (int i = 1; i <= n; i++)
{
if(i <= 2)
f = 1;
else
f = memo.get(i-1) + memo.get(i-2);
memo.put(i, f);
}
return f;
}
public static void main(String[] args)
{
int nthFib = 10;
int result = fibonacci(nthFib);
memo.clear(); // "clear cache"
int bottomUpResult = bottomUpFib(nthFib);
System.out.println(result);
System.out.println(bottomUpResult);
}
}