Climbing Stairs šŸŽÆ

beginner
22 min

Climbing Stairs šŸŽÆ

Welcome to another exciting lesson at CodeYourCraft! Today, we're going to dive into the world of Data Structures and Algorithms, focusing on a problem known as "Climbing Stairs." This problem is not only fun but also very useful in real-world programming scenarios. Let's get started!

Understanding the Problem šŸ“

Imagine you have a staircase with n steps, and each time you climb a step, you can either climb 1 or 2 steps. The question is, how many possible ways are there to reach the top of the staircase?

This problem might seem complex at first, but don't worry! We'll break it down into smaller parts and solve it together.

Solving the Problem šŸ’”

To solve this problem, we'll use a recursive approach and a memoization technique to avoid redundant calculations. Here's a simple Python function that solves the problem:

python
def climb_stairs(n): # Base cases if n <= 1: return n # If the result is already computed, return it if type(climb_stairs)[0] != int: return climb_stairs[n] # Compute the result and store it in the memo table climb_stairs[n] = climb_stairs(n - 1) + climb_stairs(n - 2) return climb_stairs[n]

Let's break down this function:

  1. We define the climb_stairs function that takes an integer n representing the number of steps in the staircase.
  2. We have two base cases: if n is less than or equal to 1, the number of ways to reach the top is n itself.
  3. We check if the result for the given n has already been computed and stored in the memo table (a Python dictionary in this case).
  4. If the result is already computed, we return it.
  5. If not, we compute the result by adding the number of ways to reach the top from the last and second-last steps.
  6. Finally, we store the computed result in the memo table for future reference.

Now, let's see this function in action!

Practical Example āœ…

python
climb_stairs = {} print(climb_stairs(4)) # Output: 5 print(climb_stairs[4]) # Output: 5

In this example, we compute the number of ways to reach the top of a staircase with 4 steps. The function tells us there are 5 possible ways to do so.

Optimization šŸ’”

The above solution works fine for small input sizes, but it's not very efficient as it involves recursive calls and a lot of redundant calculations. We can optimize this solution by using an iterative approach.

Here's the optimized Python function:

python
def climb_stairs_optimized(n): if n <= 1: return n steps = [0] * (n + 1) steps[1] = 1 steps[2] = 2 for i in range(3, n + 1): steps[i] = steps[i - 1] + steps[i - 2] return steps[n]

In this optimized version, we use an array steps to store the number of ways to reach each step. We initialize the first two elements and then iterate through the array, computing the number of ways for each step by adding the number of ways for the previous two steps.

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the number of ways to reach the top of a staircase with 5 steps, using only 1 or 2 steps at a time?

That's it for today! We hope you enjoyed learning about climbing stairs and found the problem interesting. Stay tuned for more exciting lessons on Data Structures and Algorithms at CodeYourCraft! šŸŽ‰

Want to practice more? Check out our challenges section for similar problems. Happy coding! šŸš€šŸ’»