We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 509b60f commit cb64d7eCopy full SHA for cb64d7e
Easy/Climbing Stairs.java
@@ -1,14 +1,19 @@
1
class Solution {
2
- public int climbStairs(int n) {
3
- if (n == 1) {
4
- return 1;
+ public int climbStairs(int n) {
+ Integer[] dp = new Integer[n + 1];
+ return climbStairs(dp, n);
5
}
6
- int[] dp = new int[n + 1];
7
- dp[1] = 1;
8
- dp[2] = 2;
9
- for (int i = 3; i <= n; i++) {
10
- dp[i] = dp[i - 1] + dp[i - 2];
+
+ private int climbStairs(Integer[] dp, int n) {
+ if (n == 1) {
+ return 1;
+ }
11
+ if (n == 2) {
12
+ return 2;
13
14
+ if (dp[n] != null) {
15
+ return dp[n];
16
17
+ return dp[n] = climbStairs(dp, n - 1) + climbStairs(dp, n - 2);
18
- return dp[n];
- }
19
0 commit comments