-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathStairs.java
More file actions
53 lines (45 loc) · 937 Bytes
/
Copy pathStairs.java
File metadata and controls
53 lines (45 loc) · 937 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package DynamicProgramming;
import java.util.*;
public class ClimbStairs {
public static void main(String[] args) {
// TODO Auto-generated method stub
//By using Dynamic Programming
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//int cp = countPath(n,new int[n+1]);
int cp = countPathTabulation(n);
System.out.println(cp);
}
public static int countPath(int n , int[] qb) {
if(n==0) {
return 1;
}else if(n<0) {
return 0;
}
if(qb[n] > 0) {
return qb[n];
}
int nm1 = countPath(n-1,qb);
int nm2 = countPath(n-2,qb);
int nm3 = countPath(n-3,qb);
int cp = nm1+nm2+nm3;
qb[n] = cp;
return cp;
}
public static int countPathTabulation(int n) {
int[] dp = new int[n+1];
dp[0] = 1;
for(int i =1;i<=n;i++) {
if(i==1) {
dp[i] = dp[i-1];
}
else if(i==2) {
dp[i] = dp[i-1] +dp[i-2];
}
else {
dp [i] = dp[i-1] +dp[i-2] +dp[i-3];
}
}
return dp[n];
}
}