forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_stairs.java
More file actions
38 lines (31 loc) · 709 Bytes
/
n_stairs.java
File metadata and controls
38 lines (31 loc) · 709 Bytes
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
35
36
37
38
package DynamicProgramming;
import java.util.*;
public class n_stairs {
public static int totalWays(int n) {
int[][] dp = new int[3][n + 1];
for (int i = 0; i < 3; i++) {
for (int j = 0; j <= n; j++) {
if (j == 0 || i == 1) {
dp[i][j] = 1;
} else if (i == 0) {
dp[i][j] = 0;
} else if (j < i) {
dp[i][j] = dp[i][j - 1];
} else {
dp[i][j] = dp[i][j - 1] + dp[i][j - i];
}
}
}
print(dp);
return dp[2][n];
}
public static void print(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(Arrays.toString(arr[i]));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
totalWays(4);
}
}