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 2ac6f2e

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents f7f2627 + 085ca00 commit 2ac6f2e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
6. matrix_transposition()
1111
7. lg()
1212
8. Fibonacci.fib()
13+
9. Fibonacci.fast_fib()
14+
15+
## Code Examples
16+
One interesting implementation is this algorithm to compute fibonacci sequences (exercise 1.1.19). It runs in O(1) time provided that the cache contains the previous two sequences. Generating the previous sequences is usually done using loops.
17+
```python3
18+
@staticmethod
19+
def fast_fib(n: int, cache: {int: int}) -> int:
20+
"""Fibonacci sequence algorithm utilizing dynamic programing and memoization"""
21+
if n == 0:
22+
cache[n] = 0
23+
return 0
24+
25+
if n == 1:
26+
cache[n] = 1
27+
return 1
28+
29+
cache[n] = cache[n - 2] + cache[n - 1]
30+
return cache[n]
31+
```
1332

1433
## Java Implementation
1534
These were originally implemented in Java. You can find the source [here](https://github.com/dev-xero/java-algorithms-exercise-practice)

0 commit comments

Comments
(0)

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