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 706384a commit 92f91c1Copy full SHA for 92f91c1
src/main/java/com/fishercoder/solutions/_70.java
@@ -32,22 +32,17 @@
32
33
public class _70 {
34
public static class Solution1 {
35
- //classical dp problem
36
public int climbStairs(int n) {
37
- if (n < 1) {
38
- return 0;
39
- } else if (n < 4) {
+ if (n == 1) {
40
return n;
41
- } else {
42
- int[] dp = new int[n + 1];
43
- //the number of ways to reach step n could be calculated from n-1 and n-2
44
- dp[1] = 1;
45
- dp[2] = 2;
46
- for (int i = 3; i <= n; i++) {
47
- dp[i] = dp[i - 1] + dp[i - 2];
48
- }
49
- return dp[n];
50
}
+ 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];
+ }
+ return dp[n];
51
52
53
0 commit comments