Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

[Solved] How to solve climbing stairs problem in Java? Example

In this article, we are going to be solving the stairs problem using java, After that, I shall be explaining the concept of Array. but before we start writing codes it is fine to have an understanding of the problem we are about to solve. Given the number of stairs and the number of times you can move your legs, we would see how many possible ways you can achieve the climbing of stairs. The image below shows different possible ways you can climb stairs. For instance, let us say we have 5 steps on the stairs. and you can either walk with 2 moves or 1. 

how many possible ways, let us check:

possible way one : 1,1,1,1,1. 

possible way two: 2,2,1.

possible way three: 1,2,2.

possible way four : 2,1,2.

possible was five: 1,2,2.

So there are five possible ways of climbing a stair of five steps. Maybe there may be more ways, maybe not. so with this explanation of the problem statement, I believe we now have a good understanding of the problem, now can implement it in the writing of codes.

Climbing stairs problem solution in Java
fig 1.0: possible ways of climbing stairs. (There might be more possible ways)

CODE IMPLEMENTATION

  1. public static int climbStairs(int stairSteps) {
  2. if (stairSteps <= 1) {
  3. return 1;
  4. }
  5. int[] ways = new int[stairSteps + 1];
  6. ways[0] = 1;
  7. ways[1] = 1;
  8. for (int i = 2; i <= stairSteps; i++) {
  9. ways[i] = ways[i - 2] + ways[i - 1];
  10. }
  11. return ways[stairSteps];
  12. }
The above-written codes tell us several ways we can climb stairs. A method was created in line 1 which takes n steps. that is any number of stair steps. Line 2 was a condition checking if the stair-step was lesser or equal to 1 then it should return it. of course, if a stair step is 1 the only possible way to climb is still one even if you have a hundred legs that you can use at a time. in line 5 we had our array that stores different possible ways of climbing a staircase and we initialized it with the number of stair steps. 
 
Yes, we had to add 1 to it to avoid the array index out of bounds exception for some cases. Lines 6 and 7 initialized each index to 1 and line 8 ran the for loop. In the same way, you would do Fibonacci. the final result was returned in line 11.

  1. public static void main(String[] args) {
  2. System.out.println("you have " + climbStairs(4)+
  3. " possible ways of climbing the stairs" );
  4. }
Here above is the main method, because the method "climb stairs" was a static method we do not have to instantiate or create a new instance of the class. we call the method directly. and it is been printed out.

Whenever one is faced with an algorithm challenge like this, Basically Array is very important. It is a common data structure that can be used at any time. Arrays data structure makes our work easier and provides a way of storing items in a single variable. 

Think of it that, you want to determine the smallest or greatest number, an efficient way is to have a container or variable, not just a variable but a special kind of variable mean, an array. Which you would store all your numbers in one container.

The thing about an array is that, once you declare an array at creation, you must initialize it with its length, let us say, for instance, 5. This is a way of saying that the array variable that is created, its capacity or length, or how many numbers it would take is 5. And that is the final, You won't be able to add the number 6th item because you have specified it to be 5. 

[Solved] How to solve climbing stairs problem in Java? Example


What happens under the hood in the JVM(Java Virtual Machine is that) once you declared an array and specify the length, it creates space for just the length you have specified. In our case 5, You trying to add more than 5 items gives an error, which is called Array Index Out of Bound Exception, Sorry it can't go beyond 5. If you must, then you create another array with the length of 6 or change the previous from 5 to 6.

Note: It is important to note that If you have an array and you don't populate items into it up to the length you have specified, it is very okay. But You cant populate more than what you have specified. It does not work like that with Array. 

 The code snippet shows you how an array is created and initialized. There are two ways to do this, the first one is creating an array with its length specified without populating it with items yet. The second one is populating it with items directly as you create it.

String color = new String [5];
String color = {"black","green","cyan","lemon","pink"};
Any way you choose to do this is fine, we can now move to solving the challenge of climbing stairs because Array would be needed.

CONCLUSION
 It is important to note that If you have an array and you don't populate items into it up to the length you have specified, it is very okay. But You cant populate more than what you have specified. It does not work like that with Array. If you don't have an items yet inside the array, it is initialized to 0s for integers, false for booleans 0.0 for double and null for reference types like Strings. etc.

Other Data Structure and Algorithms  articles you may like


  • 75+ Coding Interview Questions for Programmers (questions)
  • How to remove duplicate elements from the array in Java? (solution)
  • 5 Books to Learn Data Structure and Algorithms in-depth (books
  • How to reverse an array in Java? (solution)
  • 6 Dynamic Programming Courses for Programmers (best courses)
  • How to implement a recursive preorder algorithm in Java? (solution)
  • 10 (Free) Data Structure Courses for Programmers (free courses)
  • How to print leaf nodes of a binary tree without recursion? (solution)
  • 10 DS, Algo, and SQL courses to Crack Coding Interview  (courses)
  • Recursive Post Order traversal Algorithm (solution)
  • Recursive InOrder traversal Algorithm (solution)
  • Iterative PreOrder traversal in a binary tree (solution)
  • How to count the number of leaf nodes in a given binary tree in Java? (solution)
  • 10 Free Data Structure and Algorithm Courses for Programmers (courses)
  • 100+ Data Structure Coding Problems from Interviews (questions)


Thanks for reading this article so far. If you like this Climbing stairs problem solution in Java then please share it with your friends and colleagues. If you have any issues, please drop a note.


1 comment:

Feel free to comment, ask questions if you have any doubt.