- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpstair.java
More file actions
Latest commit
22 lines (20 loc) · 608 Bytes
/
Copy pathUpstair.java
File metadata and controls
22 lines (20 loc) · 608 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** Dynamic programming
*
* The number of case we can climb stairs when we can go up one staircase, two staircases, and three staircases at a time
*/
publicclassUpstair {
publicstaticvoidmain(String[] args){
intn = 5;
intarr[] = newint[n+1];
System.out.println(upstair(n, arr)); // print: 13
}
publicstaticintupstair(intn, intarr[]){
if (n <= 1)
return1;
if (n == 2)
return2;
if (arr[n] == 0)
arr[n] = upstair(n-1, arr) + upstair(n-2, arr) + upstair(n-3, arr);
returnarr[n];
}
}