[Path] leetcode white brush title of Day1

The classic dynamic programming entry title - jump stairs questions submitted three or four times in a row does not pass, it recorded lessons.

[Jump] steps

Suppose you are climbing stairs. N order you need to get to the roof.

Every time you can climb one or two steps. How many different ways can climb to the roof of it?

Note: Given n is a positive integer.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/climbing-stairs
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Submit a question of code:

class Solution:
    def climbStairs(self, n: int) -> int:
        dp = [i for i in range(n+1)]
        
        i = 2
        while i > 2 and i < n:
            dp[i] = dp[i-1] + dp[i-2]
        
        return dp[n]

 The first two elements dp [1] = 1 dp [2] = 2

Therefore initialized dp [i] = i

 

There are three problems:

1. while i > 2 and i < n:

3 while the recursion starts, i initialized to 1, while the code will not be executed

2. After each execution of the code while the body, i without updating the formula given i + = 1, while the inner code is simply not iteration

3. while loop termination condition is: i <n

Will only obtain dp [i-1],

Guess you like

Origin www.cnblogs.com/ACStrive/p/11440014.html