Skip to content

Instantly share code, notes, and snippets.

@laalaguer
Created January 19, 2022 05:54
Show Gist options
  • Save laalaguer/db3bd0582bfef0303387fcb000bb6410 to your computer and use it in GitHub Desktop.
Save laalaguer/db3bd0582bfef0303387fcb000bb6410 to your computer and use it in GitHub Desktop.
# Speed up.
cache = {}
def f(n):
# Speed up.
if cache.get(n) != None:
return cache[n]
if n == 1:
print('n==1, directly return 1')
return 1
p = [] # Storage for temp results.
for i in range(1, n): # i in [1, n)
temp = 1 + max(i-1, f(n-i))
p.append(temp)
# Speed up.
cache[n] = min(p)
return min(p)
print('f(100)', f(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment