Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca6d8fc

Browse files
Create Stair Case
1 parent 1210aaf commit ca6d8fc

File tree

1 file changed

+60
-0
lines changed
  • Course 2 - Data Structures in JAVA/Lecture 18 - Dynamic Programming I

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
A child runs up a staircase with 'n' steps and can hop either 1 step, 2 steps or 3 steps at a time.
3+
Implement a method to count and return all possible ways in which the child can run-up to the stairs.
4+
5+
Input format :
6+
The first and the only line of input contains an integer value, 'n', denoting the total number of steps.
7+
8+
Output format :
9+
Print the total possible number of ways.
10+
11+
Constraints :
12+
0 <= n <= 10 ^ 4
13+
Time Limit: 1 sec
14+
15+
Sample Input 1:
16+
4
17+
Sample Output 1:
18+
7
19+
20+
Sample Input 2:
21+
10
22+
Sample Output 2:
23+
274
24+
*/
25+
public class Solution {
26+
27+
public static long staircase(int n) {
28+
//Your code goes here
29+
/*
30+
//If we reach n=0, we have found a path
31+
if (n==0)
32+
return 1;
33+
34+
//If n<0, the steps we took till then don't work
35+
else if(n<0)
36+
return 0;
37+
38+
return staircase(n-1)+staircase(n-2)+staircase(n-3);
39+
*/
40+
41+
if(n<=1)
42+
return 1;
43+
if(n==2)
44+
return 2;
45+
46+
long dpCount[] = new long[n+1];
47+
dpCount[0]=1;
48+
dpCount[1]=1;
49+
dpCount[2]=2;
50+
51+
52+
for (int i=3;i<=n;i++)
53+
{
54+
dpCount[i]=dpCount[i-1]+dpCount[i-2]+dpCount[i-3];
55+
56+
}
57+
return dpCount[n];
58+
}
59+
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /