-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunningSteps.java
More file actions
30 lines (25 loc) · 629 Bytes
/
Copy pathRunningSteps.java
File metadata and controls
30 lines (25 loc) · 629 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
// A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps
// at a time. Implement a method to count how many possible ways the child can run up the stairs.
class RunningSteps {
public static void main(String arg[]) {
RunningSteps s = new RunningSteps();
int n = 4;
System.out.println(s.count(n));
}
int count (int n) {
int count = 0;
if (n == 1) {
count = 1;
}
if (n == 2) {
count = 2;
}
if (n == 3) {
count = 4;
}
if (n > 3) {
count = count(n - 1) + count(n - 2) + count(n - 3);
}
return count;
}
}