-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctWaysClimbStairs.java
More file actions
34 lines (21 loc) · 1.07 KB
/
Copy pathDistinctWaysClimbStairs.java
File metadata and controls
34 lines (21 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
class DistinctWaysClimbStairs {
// Static method to calculate the distinct ways to climb stairs
public static int climbStairs(int n) {
if (n <= 1) {
return 1; // If there is 0 or 1 step, there is only 1 way to climb.
}
int[] s_case = new int[n + 1]; // Create an array to store the number of distinct ways for each step count.
s_case[0] = 1; // There is 1 way to climb 0 steps.
s_case[1] = 1; // There is 1 way to climb 1 step.
for (int i = 2; i <= n; i++) {
// Calculate the number of distinct ways for each step by adding the ways from the previous two steps.
s_case[i] = s_case[i - 1] + s_case[i - 2];
}
return s_case[n]; // Return the number of distinct ways to climb n steps.
}
public static void main(String[] args) {
int steps = 5; // The number of steps to climb
System.out.println("Distinct ways can you climb to the top: " + climbStairs(steps));
}
}