File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
main/java/com/thealgorithms/dynamicprogramming
test/java/com/thealgorithms/dynamicprogramming Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .dynamicprogramming ;
2
+
3
+ /* A DynamicProgramming solution for Climbing Stairs' problem Returns the
4
+ distinct ways can you climb to the staircase by either climbing 1 or 2 steps.
5
+
6
+ Link : https://medium.com/analytics-vidhya/leetcode-q70-climbing-stairs-easy-444a4aae54e8
7
+ */
8
+ public class ClimbingStairs {
9
+
10
+ public static int numberOfWays (int n ) {
11
+
12
+ if (n == 1 || n == 0 ){
13
+ return n ;
14
+ }
15
+ int prev = 1 ;
16
+ int curr = 1 ;
17
+
18
+ int next ;
19
+
20
+ for (int i = 2 ; i <= n ; i ++){
21
+ next = curr +prev ;
22
+ prev = curr ;
23
+
24
+ curr = next ;
25
+ }
26
+
27
+ return curr ;
28
+
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .dynamicprogramming ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+
5
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6
+
7
+
8
+ public class climbStairsTest {
9
+
10
+ @ Test
11
+ void climbStairsTestForTwo (){assertEquals (2 , ClimbingStairs .numberOfWays (2 ));}
12
+
13
+ @ Test
14
+ void climbStairsTestForZero (){assertEquals (0 , ClimbingStairs .numberOfWays (0 ));}
15
+
16
+ @ Test
17
+ void climbStairsTestForOne (){assertEquals (1 , ClimbingStairs .numberOfWays (1 ));}
18
+
19
+ @ Test
20
+ void climbStairsTestForFive (){assertEquals (8 , ClimbingStairs .numberOfWays (5 ));}
21
+
22
+ @ Test
23
+ void climbStairsTestForThree (){assertEquals (3 , ClimbingStairs .numberOfWays (3 ));}
24
+ }
You can’t perform that action at this time.
0 commit comments