-
-
Save anonymous/d23efa6ad0c662c9d261 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
| private static final Map<Integer, Long> fibMap = new HashMap<Integer, Long>(); | |
| //Is storing the result dynamic programming? | |
| private static long fibonacci(int n, boolean useMap) { | |
| if (n <= 0) | |
| return 1; | |
| else if (n == 1) | |
| return 2; | |
| else if (useMap) { | |
| if (fibMap.containsKey(n)) | |
| return fibMap.get(n); | |
| else { | |
| fibMap.put(n, fibonacci(n - 1, useMap) + fibonacci(n - 2, useMap)); | |
| return fibMap.get(n); | |
| } | |
| } else | |
| return fibonacci(n - 1, useMap) + fibonacci(n - 2, useMap); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment