Files
hello-algo/en/codes/python/chapter_dynamic_programming/climbing_stairs_dfs.py
T
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

29 lines
585 B
Python

"""
File: climbing_stairs_dfs.py
Created Time: 2023-06-30
Author: krahets ([email protected])
"""
def dfs(i: int) -> int:
"""Search"""
# The values of dp[1] and dp[2] are known; return the corresponding value
if i == 1 or i == 2:
return i
# dp[i] = dp[i-1] + dp[i-2]
count = dfs(i - 1) + dfs(i - 2)
return count
def climbing_stairs_dfs(n: int) -> int:
"""Climbing stairs: search"""
return dfs(n)
"""Driver code"""
if __name__ == "__main__":
n = 9
res = climbing_stairs_dfs(n)
print(f"Number of ways to climb {n} stairs: {res}")