-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibSeries.java
More file actions
60 lines (53 loc) · 1.61 KB
/
Copy pathFibSeries.java
File metadata and controls
60 lines (53 loc) · 1.61 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
package DynamicProgramming;
public class FibSeries {
static int fibOne(int n) {
if(n == 0 || n == 1) {
return n;
}
int first = fibOne(n - 1);
int second = fibOne(n - 2);
int result = first + second;
return result;
}
// Memoization
static int fib(int n, int []cache) {
if(n == 0 || n == 1) {
return n;
}
if(cache[n] != 0) {
return cache[n];
}
int first = fib(n - 1, cache);
int second = fib(n - 2, cache);
int result = first + second;
cache[n] = result;
return result;
}
// Tabulation
static int fibTab(int n, int []cache) {
cache[0] = 0;
cache[1] = 1;
for(int i = 2; i <= n; i++) {
cache[i] = cache[i-1] + cache[i-2];
}
return cache[n];
}
public static void main(String[] args) {
int n = 100;
long start = System.currentTimeMillis();
int result = fibOne(n);
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("Without DP");
System.out.println(result + " time taken : " + total);
int cache[] = new int[n + 1];
start = System.currentTimeMillis();
result = fib(n, cache);
end = System.currentTimeMillis();
total = end - start;
System.out.println("With DP");
System.out.println(result + " time taken : " + total);
result = fibTab(n, cache);
System.out.println(result);
}
}