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.
fig 1.0: possible ways of climbing stairs. (There might be more possible ways)CODE IMPLEMENTATION
- public static int climbStairs(int stairSteps) {
- if (stairSteps <= 1) {
- return 1;
- }
- int[] ways = new int[stairSteps + 1];
- ways[0] = 1;
- ways[1] = 1;
- for (int i = 2; i <= stairSteps; i++) {
- ways[i] = ways[i - 2] + ways[i - 1];
- }
- return ways[stairSteps];
- }
- public static void main(String[] args) {
- System.out.println("you have " + climbStairs(4)+
- " possible ways of climbing the stairs" );
- }
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.
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.
possible way three: 1,2,2
ReplyDeletepossible was five: 1,2,2
-_-