Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ec1de4

Browse files
DOCS Added code examples section
1 parent 604c98c commit 4ec1de4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,25 @@
1515
11. binarySearch()
1616
12. bruteForceSearch()
1717

18+
## Code Examples
19+
We are asked in exercise (1.1.19) to improve the fib method to use some sort of cache. This one is particularly interesting because it runs in O(1) time provided that the cache contains the previous two sequences. Generating the previous sequences is done using loops.
20+
```java
21+
// dynamic programming and memoization
22+
public static long FastFib(int N, HashMap<Integer, Long> cache) {
23+
if (N == 0) {
24+
cache.put(N, 0L);
25+
return 0;
26+
}
27+
28+
if (N == 1) {
29+
cache.put(N, 1L);
30+
return 1;
31+
}
32+
33+
cache.put(N, cache.get(N - 2) + cache.get(N - 1));
34+
return cache.get(N);
35+
}
36+
```
37+
1838
## Python Implementation
1939
I've implemented the same methods but in Python over [here](https://github.com/dev-xero/python-algorithms-exercise-practice), if you're interested.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /