-
Notifications
You must be signed in to change notification settings - Fork 43
/
ClimbingStairs.java
119 lines (97 loc) · 2.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package LeetCodeJava.DynamicProgramming;
// https://leetcode.com/problems/climbing-stairs/
import java.util.*;
public class ClimbingStairs {
// V0
// IDEA : DP
public int climbStairs(int n) {
if (n <= 2){
if (n == 1){
return 1;
}
return 2;
}
/**
*
* 0, 1, 2, 3..... k-2, k-1, k
* dp[k] = dp[k-2] + dp[k-1]
*
*/
// init dp
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = 2;
for (int i = 2; i < n; i++){
dp[i] = dp[i-2] + dp[i-1];
//System.out.println("dp[i] = " + dp[i]);
}
return dp[n-1];
}
// V0
// IDEA : DP
public int climbStairs_0(int n) {
// null check
if (n <= 0){
return 0;
}
if (n == 1 || n == 2){
return n;
}
// NOTE !!! init dp as ArrayList
List<Integer> dp = new ArrayList<>();
dp.add(1); // i = 0
dp.add(1); // i = 1
//System.out.println("dp = " + dp);
// NOTE !!! start from i = 2
for (int i = 2; i <= n; i++){
int tmp = dp.get(i-1) + dp.get(i-2);
dp.add(tmp);
}
return dp.get(n);
}
// V1
// IDEA : Memoization
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/
public int climbStairs_2(int n) {
Map<Integer, Integer> memo = new HashMap<>();
return climbStairs(n, memo);
}
private int climbStairs(int n, Map<Integer, Integer> memo) {
if (n == 0 || n == 1) {
return 1;
}
if (!memo.containsKey(n)) {
memo.put(n, climbStairs(n-1, memo) + climbStairs(n-2, memo));
}
return memo.get(n);
}
// V2
// IDEA : Tabulation
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/
public int climbStairs_4(int n) {
if (n == 0 || n == 1) {
return 1;
}
int[] dp = new int[n+1];
dp[0] = dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
// V3
// IDEA : Space Optimization
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/
public int climbStairs_5(int n) {
if (n == 0 || n == 1) {
return 1;
}
int prev = 1, curr = 1;
for (int i = 2; i <= n; i++) {
int temp = curr;
curr = prev + curr;
prev = temp;
}
return curr;
}
}