Skip to content

Instantly share code, notes, and snippets.

@MrMeison
Created June 16, 2018 08:47
Show Gist options
  • Save MrMeison/843cb5b71fabceef361867862ddeb6d7 to your computer and use it in GitHub Desktop.
Save MrMeison/843cb5b71fabceef361867862ddeb6d7 to your computer and use it in GitHub Desktop.
def step(mins, n):
if n == 1:
return 0
if (mins[n] != -1):
return mins[n]
result = 1 + step(mins, n-1)
if (n % 2 == 0):
result = min(result, 1 + step(mins, n // 2))
if (n % 3 == 0):
result = min(result, 1 + step(mins, n // 3))
mins[n] = result
return result
def solve (n):
mins = [-1] * (n+1)
return step(mins, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment