Level
Find the error
-
def climb_stairs(n):
-
    if n <= 2:
-
        return n
-
    dp = [0] * (n + 1)
-
    dp[1] = 1
-
    dp[2] = 2
-
    for i in range(3, n):
-
        dp[i] = dp[i-1] + dp[i-2]
-
    return dp[n]
-
print(climb_stairs(5))

LeetCode 70: Climbing Stairs

In this LeetCode-style problem, you need to implement a function that calculates the number of ways to climb n stairs. Identify the line with a logical error in the loop range and select the correct implementation to fix it. Pay attention to how the dynamic programming array is filled and accessed.