Created
September 22, 2017 18:51
-
-
Save lisamariewatkins/2cd5b3d772175101019c892d9f780b20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // bottom up (tabulation) | |
| static int fibBottomUp(int n){ | |
| int[] fib = new int[n + 1]; | |
| fib[0] = 1; | |
| fib[1] = 1; | |
| for(int i = 2; i <= n; i++){ | |
| fib[i] = fib[i - 2] + fib[i - 1]; | |
| } | |
| return fib[n]; | |
| } | |
| // top down (memoization) | |
| static int fibTopDown(int n){ | |
| int[] fib = new int[n + 1]; | |
| if(fib[n] == null){ | |
| if(n <= 1){ | |
| fib[n] = 1; | |
| } | |
| else{ | |
| fib[n] = fibTopDown(n - 2) + fibTopDown(n-1); | |
| } | |
| } | |
| return fib[n]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment