diff --git a/math_logic_dp/climbing_stairs.py b/math_logic_dp/climbing_stairs.py index 0b3f0b8..6a8a76c 100644 --- a/math_logic_dp/climbing_stairs.py +++ b/math_logic_dp/climbing_stairs.py @@ -1,2658 +1,56 @@ +''' +You are climbing a staircase. It takes n steps to reach the top. +Each time you can either climb 1 or 2 steps. +In how many distinct ways can you climb to the top? +''' + + +def climb_stairs_o2n(n: int) -> int: + if n == 0 or n == 1: + return 1 + return climb_stairs_o2n(n-1) + climb_stairs_o2n(n-2) +def helper(n: int, memo: dict[int, int]) -> int: + if n == 0 or n == 1: + return 1 + if n not in memo: + memo[n] = helper(n-1, memo) + helper(n-2, memo) + return memo[n] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Page not found · GitHub - - - - +def climb_stairs_memoization(n: int) -> int: + memo = {} + return helper(n, memo) + - - - - - - - - - - - - - - - - - - - - - - - - - - +def climb_stairs_optimized(n: int) -> int: + if n == 0 or n == 1: + return 1 + prev, curr = 1, 1 + for i in range(2, n+1): + temp = curr + curr = prev + curr + prev = temp + return curr +def climb_stairs_tabulation(n: int) -> int: + if n == 0 or n == 1: + return 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
- Skip to content - - - - - - - - - - - - -
-
-
- - - - - -
- -
-
-
-

- Global navigation -

-
-
-
-
- -
-
-
-
- -
- - - - -
-
- -
- -
-
- - - -
-
-
- -
- -
-
- -
-
- - -
- - -
Create new... -
- - -
- - -
- -
- -
-
- -
- - Issues -
-
- - Pull requests -
-
- -
- - - - - - - - - - - Notifications - -
- - - -
- - - - - -
- -
-
-
-

- Account menu -

-
-
- - - marina-google - - - - Marina S. - -
-
-
-
-
- -
-
-
-
- - -
- -
-
- -
-
-
- -
- - - - - -
-
-
- - -
- - - - -
- -
- - - - - - - - -
- - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- -
- - - - marina-google  /   - -
-
- - - -
- - -
-
- Clear Command Palette -
-
- - - -
-
- Tip: - Type # to search pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type # to search discussions -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type ! to search projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search teams -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type @ to search people and organizations -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type > to activate command mode -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Go to your accessibility settings to change your keyboard shortcuts -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type author:@me to search your content -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:pr to filter to pull requests -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:issue to filter to issues -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:project to filter to projects -
-
- Type ? for help and tips -
-
-
- -
-
- Tip: - Type is:open to filter to open content -
-
- Type ? for help and tips -
-
-
- -
- -
-
- We’ve encountered an error and some results aren't available at this time. Type a new search or try again later. -
-
- - No results matched your search - - - - - - - - -
- - - - - Search for issues and pull requests - - # - - - - Search for issues, pull requests, discussions, and projects - - # - - - - Search for organizations, repositories, and users - - @ - - - - Search for projects - - ! - - - - Search for files - - / - - - - Activate command mode - - > - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Search your issues, pull requests, and discussions - - # author:@me - - - - Filter to pull requests - - # is:pr - - - - Filter to issues - - # is:issue - - - - Filter to discussions - - # is:discussion - - - - Filter to projects - - # is:project - - - - Filter to open issues, pull requests, and discussions - - # is:open - - - - - - - - - - - - - - - - -
-
-
- -
- - - - - - - - - - -
- - -
-
- - -
-
- -
-
- 404 “This is not the web page you are looking for” - - - - - - - - - - - - -
-
- -
-
- -
- - -
-
- -
- - -
- -
- - - - - - - - - - - - - - - - - - - -
- -
- - +if __name__ == "__main__": + print(climb_stairs_o2n(4)) + print(climb_stairs_memoization(4)) + print(climb_stairs_optimized(4)) + print(climb_stairs_tabulation(4))