-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (29 loc) · 788 Bytes
/
Solution.java
File metadata and controls
34 lines (29 loc) · 788 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
import java.util.*;
public class Solution {
public static int numWays(int n) {
if (n < 3) {
return n;
}
if (n == 3) {
return 4;
}
int[] numWays = new int[n];
numWays[0] = 1;
numWays[1] = 2;
numWays[2] = 4;
for (int i = 3; i < n; i++) {
numWays[i] = numWays[i - 1] +
numWays[i - 2] +
numWays[i - 3];
}
return numWays[n - 1];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
while (in.hasNext()) {
int staircaseHeight = in.nextInt();
System.out.println(numWays(staircaseHeight));
}
}
}