-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
52 lines (48 loc) · 1.18 KB
/
ClimbingStairs.java
File metadata and controls
52 lines (48 loc) · 1.18 KB
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
package algorithm.lc;
/**
* You are climbing a stair case. It takes n steps to reach to the top.
*
* Each time you can either climb 1 or 2 steps. In how many distinct ways can
* you climb to the top?
*
*/
public class ClimbingStairs {
// 1D DP
// O(n) space, O(n) time
public class Solution {
public int climbStairs(int n) {
// Start typing your Java solution below
// DO NOT write main() function
if (n == 0) {
return 0;
}
int[] ways = new int[n + 1];
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i < ways.length; ++i) {
ways[i] = ways[i - 1] + ways[i - 2];
}
return ways[n];
}
}
// O(1) space, O(n) time
public class Solution2 {
public int climbStairs(int n) {
// Start typing your Java solution below
// DO NOT write main() function
if (n == 0) {
return 1;
}
else if (n == 1) {
return 1;
}
int[] ways = new int[2];
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i < n + 1; ++i) {
ways[(i - 2) % 2] = ways[((i - 2) % 2 + 1) % 2] + ways[(i - 2) % 2];
}
return ways[(n - 2) % 2];
}
}
}