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 ebc167f commit ee812baCopy full SHA for ee812ba
Easy/Climbing Stairs.java
@@ -1,20 +1,14 @@
1
class Solution {
2
- int[] memo;
3
public int climbStairs(int n) {
4
if (n == 1) {
5
return 1;
6
}
7
- memo = new int[n + 1];
8
- memo[1] = 1;
9
- memo[2] = 2;
10
- return helper(n);
11
- }
12
-
13
- private int helper(int n) {
14
- if (memo[n] != 0) {
15
- return memo[n];
+ int[] dp = new int[n + 1];
+ dp[1] = 1;
+ dp[2] = 2;
+ for (int i = 3; i <= n; i++) {
+ dp[i] = dp[i - 1] + dp[i - 2];
16
17
- memo[n] = helper(n - 1) + helper(n - 2);
18
+ return dp[n];
19
20
0 commit comments