Skip to content
Snippets Groups Projects
Commit d297a0cf authored by Martin Mareš's avatar Martin Mareš
Browse files

DP: Ukážeme @cache

parent 2c50c606
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,15 @@ def d2(n):
pamet[n] = d2(n-1) + d2(n-2)
return pamet[n]
# Kešování umí Python zařídit i sám.
from functools import cache
@cache
def d2b(n):
if n <= 1:
return 1
return d2(n-1) + d2(n-2)
# Tady počítáme tytéž podproblémy od nejmenšího k největšímu.
# Rekurzi jsme nahradili obyčejným cyklem, složitost je evidentně O(n).
def d3(n):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment