-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabulation.java
More file actions
36 lines (32 loc) · 829 Bytes
/
Tabulation.java
File metadata and controls
36 lines (32 loc) · 829 Bytes
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
package algorithm.DynamicProgramming;
/**
* The type Tabulation.
*/
class Tabulation
{
/**
* Fib int.
*
* @param n the n
* @param lookupTable the lookup table
* @return the int
*/
public static int fib(int n, int[] lookupTable)
{
lookupTable[0] = 0; // Set zeroth and first values
lookupTable[1] = 1;
for (int i = 2; i <= n; i++)
lookupTable[i] = lookupTable[i-1] + lookupTable[i-2]; // Fill up the table by summing up previous two values
return lookupTable[n]; // Return the nth Fibonacci number
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
int n = 6;
int[] lookupTable = new int[n+1];
System.out.print(fib(n, lookupTable));
}
}