The description of the question is in the following link:
https://leetcode.com/problems/climbing-stairs/
My Code:
class Solution:
def climbStairs(self, n: int) -> int:
Ladder = [1]*n;
if n ==1:
return 1;
else:
Ladder[1] = 2;
for LadNum in range(2,n):
Ladder[LadNum] = Ladder[LadNum-1]+Ladder[LadNum -2]
return Ladder[n-1]
Explanation:
The idea is clear: the only way to reach the nth ladder is that going from n-1th ladder with 1 step and n-2th ladder with 2 steps. So we just need to sum the number of steps to reach these two ladders to get the step to reach nth ladder. Just need to be very careful about some special situation and initialisation.