-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathClimbingStairs.java
61 lines (48 loc) · 1.2 KB
/
ClimbingStairs.java
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
54
55
56
57
58
59
60
61
package problems.leetcode;
import java.util.Arrays;
/**
* https://leetcode.com/problems/climbing-stairs/
* <p>
* dynamic programming
*/
public class ClimbingStairs {
private int[] counts;
public int climbStairs(int n) {
counts = new int[n];
Arrays.fill(counts, -1);
return climbStairs(n, 0);
}
private int climbStairs(int n, int i) {
if (i == n) {
return 1;
} else if (i > n) {
return 0;
}
if (counts[i] != -1) {
return counts[i];
}
int count = climbStairs(n, i + 1) + climbStairs(n, i + 2);
counts[i] = count;
return count;
}
public int climbStairsDP(int n) {
int[] counts = new int[n + 2];
counts[n] = 1;
for (int i = n - 1; i >= 0; i--) {
counts[i] = counts[i + 1] + counts[i + 2];
}
return counts[0];
}
public static void main(String[] args) {
int n = 1;
System.out.println(new ClimbingStairs().climbStairs(n));
System.out.println(new ClimbingStairs().climbStairsDP(n));
/*
1 1 1 1
1 2 1
1 1 2
2 1 1
2 2
*/
}
}