forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciNumbers.java
42 lines (37 loc) · 935 Bytes
/
FibonacciNumbers.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
package me.ramswaroop.dynamicprogramming;
import java.util.Arrays;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 9/10/15
* @time: 3:57 PM
*/
public class FibonacciNumbers {
/**
* Computes first {@param k} fibonacci numbers using
* bottom-up DP approach.
* <p/>
* Time complexity: O(k)
*
* @param k
*/
public static int[] getFirstKFibonacciNumbers(int k) {
int[] fib = new int[k + 1];
int i = 1;
while (i <= k) {
if (i == 1 || i == 2) {
fib[i] = 1;
} else {
fib[i] = fib[i - 1] + fib[i - 2];
}
i++;
}
return fib;
}
public static void main(String a[]) {
out.println(Arrays.toString(getFirstKFibonacciNumbers(10)));
out.println(Arrays.toString(getFirstKFibonacciNumbers(46)));
}
}