-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathFibonacci.java
72 lines (67 loc) · 1.23 KB
/
Fibonacci.java
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
68
69
70
71
72
package chapter08RecursionAndDynamicProgramming;
/**
*
* Problem: Compute the Nth Fibonacci number.
*
*/
public class Fibonacci {
/**
* Method 1: Recursive
*
* Time Complexity: O(2N), where N is the number of nodes.
*/
public int fibonacci1(int n) {
if (n == 0 || n == 1) {
return n;
}
return fibonacci1(n - 1) + fibonacci1(n - 2);
}
/**
* Method 2: Top-Down Dynamic Programming (or Memorization)
*
* Time Complexity: O(N)
*/
public int fibonacci2(int n) {
return helper(n, new int[n + 1]);
}
private int helper(int n, int[] memo) {
if (n == 0 || n == 1) {
return n;
}
if (memo[n] == 0) {
memo[n] = helper(n - 1, memo) + helper(n - 2, memo);
}
return memo[n];
}
/**
* Method 3: Bottom-Up Dynamic Programming
*/
public int fibonacci3(int n) {
if (n == 0 || n == 1) {
return n;
}
int[] memo = new int[n];
memo[0] = 0;
memo[1] = 1;
for (int i = 2; i < n; i++) {
memo[i] = memo[i - 1] + memo[i - 2];
}
return memo[n - 1] + memo[n - 2];
}
/**
* Method 4: Optimize Method3
*/
public int fibonacci4(int n) {
if (n == 0 || n == 1) {
return n;
}
int a = 0;
int b = 1;
for (int i = 2; i < n; i++) {
int c = a + b;
a = b;
b = c;
}
return a + b;
}
}