#Simangaliso Collin Mavuso collinmavuso98@gmail.com #QUESTION 2 #On this Question we used dynamic programming to solve the task where by we applied a bottom #up approach ,we could have solved it using recursion but that becomes slow as the number of steps #increases. def climbStairs(numberOfSteps): path={0: 1,1: 1}# we default the first 2 steps because they are straignt forward # since our constraint is 1>numberOfSteps<20 we can ignore 0 for x in range(2,numberOfSteps+1):#we start our count from 2 since 1 is a default value path[x]=path[x-1]+path[x-2] #in the bottom up approach we take the addition of the 2 previous steps using the fibbonaci strategy return path[numberOfSteps] #TEST CASES print(climbStairs(4)) print(climbStairs(20))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question