Leetcode 70: Climbing Stairs
You need to climb a ladder with n steps to reach the top. Each time, you can either climb 1 step or 2 steps. Find the number of distinct ways to climb to the top.
Problem
Approach
Steps
Complexity
Input: A single integer n, representing the total number of steps in the ladder.
Example: Input: n = 4
Constraints:
• 1 <= n <= 45
Output: Return an integer representing the total number of distinct ways to climb the ladder.
Example: Output: 5
Constraints:
• The output must be a valid integer that corresponds to the number of distinct ways.
Goal: Calculate the total number of ways to climb the ladder using a dynamic programming approach.
Steps:
• Define an array dp, where dp[i] represents the number of ways to climb to the ith step.
• Initialize dp[0] = 1 (1 way to stay at the start) and dp[1] = 1 (1 way to climb the first step).
• Iteratively calculate dp[i] for 2 <= i <= n using the relation dp[i] = dp[i-1] + dp[i-2].
• Return dp[n] as the result.
Goal: The input value n must meet the specified constraints, and the function must efficiently compute results within the range.
Steps:
• 1 <= n <= 45
• Handle inputs efficiently to avoid performance issues.
Assumptions:
• The input n is a valid integer.
• There are no additional environmental constraints affecting the problem.
• Input: Input: n = 4
• Explanation: The number of distinct ways to climb 4 steps is 5, as shown in the example above.
• Input: Input: n = 5
• Explanation: The number of distinct ways to climb 5 steps is 8, as shown in the example above.
Approach: Use dynamic programming to calculate the number of ways to climb the ladder iteratively.
Observations:
• The problem can be broken down recursively: the number of ways to climb n steps depends on the number of ways to climb n-1 and n-2 steps.
• This is similar to the Fibonacci sequence.
• Use an iterative approach with an array to store intermediate results for efficiency.
Steps:
• Initialize an array dp of size n+1, where dp[i] represents the number of ways to climb i steps.
• Set dp[0] = 1 and dp[1] = 1 as the base cases.
• Iteratively compute dp[i] for i = 2 to n using the formula dp[i] = dp[i-1] + dp[i-2].
• Return dp[n] as the final result.
Empty Inputs:
• Not applicable since the input n is guaranteed to be within the constraints.
Large Inputs:
• n = 45, the upper limit of the constraint.
Special Values:
• n = 1, the smallest number of steps.
Constraints:
• Ensure the implementation works for both small and large values of n within the range.
int climbStairs(int n) {
if (n <= 2) {
return n;
}
int first = 1, second = 2;
for (int i = 3; i <= n; i++) {
int third = first + second;
first = second;
second = third;
}
return second;
}
1 : Function Declaration
int climbStairs(int n) {
This line declares a function named `climbStairs` that takes an integer `n` representing the number of stairs as input and returns the number of distinct ways to climb them.
2 : Base Cases
if (n <= 2) {
return n;
}
This block handles the base cases: if `n` is 1 or 2, there's only one way to climb the stairs.
3 : Initialize Variables
int first = 1, second = 2;
Initialize `first` and `second` to represent the number of ways to climb 1 and 2 stairs, respectively.
4 : Iterative Calculation
for (int i = 3; i <= n; i++) {
This loop iterates from 3 to `n`, calculating the number of ways to climb `i` stairs.
5 : Calculate Ways to Climb i Stairs
int third = first + second;
Calculate the number of ways to climb `i` stairs by adding the ways to climb `i-1` and `i-2` stairs.
6 : Update Variables
first = second;
second = third;
Update `first` and `second` for the next iteration.
7 : Return Result
return second;
After the loop, `second` will hold the number of ways to climb `n` stairs, so return it.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: Each step from 2 to n is computed once.
Best Case: O(1) with optimization (using two variables instead of an array).
Worst Case: O(n)
Description: The space required depends on whether an array or two variables are used.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus